#INTRO: LOADING PACKAGES ETC----
library(foreign)
library(reshape2)
library(data.table)
library(stargazer)
library(MatchIt)
#library(nonrandom)
library(ggplot2)
#library(gdata1)
library(gmodels)
library(gridExtra)
library(haven)
library(plyr)
#library(car)
#library(xlsx)
library(tidyr)
library(reshape)
library(countrycode)
#library(dGlyr)
library(readr)
library(gnm)
library(MNP)
library(nls2)
library(nlstools)
library(stats)
library(dplyr)
library(matrixStats)
library(miceadds)
library(minpack.lm)
library(lfe)
library(lubridate)
library(AER)
library(reshape)
library(reshape2)
library(tictoc)
library(xtable)
library(Formula)
library(mlogit)
library(plm)
library(survival)
library(plotly)
library(alpaca)
library(lmtest)
library(sandwich)
# library(mnlogit)

library(foreach)#parallel foreach
library(iterators)
library(parallel)
library(doParallel)

library(msm)
library(tikzDevice)

library(ggpubr)#combine ggplot graphs together

library(estimatr)#lm_robust

library(clubSandwich)#coef_test

library(sjlabelled)#labelled variables (set_labels)

library(openxlsx)#read.xlsx

library(labelled)#change labels to columns

library(texreg)#extract for lm.cluster

options(width=2000)#set max characters shown
options(max.print=200000)#set max rows shown console

# !diagnostics suppress=Dsvr,Dgdm

rm(list=ls())
cat("\014")

#READ DATA:----
load("d29.RData")


#ASSIGN WEIGHTS:----

#assign Weight Country:
#calculate country (weighted by country population) weights:
# de = de %>% group_by(Es) %>% mutate(Resp = n()/6 )#number of respondent by Es
de$Pop=NA#country population in millions
de$Pop[de$Ec=="AUS"]=25
de$Pop[de$Ec=="AUT"]=9
de$Pop[de$Ec=="CAN"]=37
de$Pop[de$Ec=="DEU"]=68#West Germany (84 All Germany)
de$Pop[de$Ec=="DNK"]=6
de$Pop[de$Ec=="ESP"]=47
de$Pop[de$Ec=="FIN"]=6
de$Pop[de$Ec=="GBR"]=61
de$Pop[de$Ec=="GRC"]=10
de$Pop[de$Ec=="IRL"]=5
de$Pop[de$Ec=="ISL"]=1
de$Pop[de$Ec=="ISR"]=9
de$Pop[de$Ec=="ITA"]=61
de$Pop[de$Ec=="NLD"]=17
de$Pop[de$Ec=="NOR"]=5
de$Pop[de$Ec=="NZL"]=5
de$Pop[de$Ec=="PRT"]=10
de$Pop[de$Ec=="SWE"]=10
de$Rwp=de$Pop#Respondent Weight Country
de$Rwp=de$Rwp/mean(de$Rwp)#set mean weight == 1

#calculate country weights (weights s.t. all elections same number of respondents):
de = de %>% group_by(Es) %>% mutate(Resp = n()/6 )#number of respondent by Es
de$Rwc=1000/de$Resp#Respondent Weight Country
de$Rwc=de$Rwc/mean(de$Rwc)#set mean weight == 1
summary(de$Rwc)

#set demographic weights to average 1 by election:
# (since I deleted respondents w/o Va,Pl,Ll, they unbalance the weights for that election
# in a way that is election specific, in this way I reset sum weghts to be 1 by election)
de = de %>% group_by(Es) %>% mutate(Rwd=Rwd/mean(Rwd))
summary(de$Rwd)

#calculate country demographic weights:
# NB: this is equivalent to CSES Dataset Weight:
# | The derivative "Dataset Weight" (D1014) has been created so
# | that each election study in the dataset will contribute
# | equally to analyses of respondents, regardless of the number
# | of interviews in each election study.
de$Rwcd=de$Rwc*de$Rwd#Respondent Weight Country and Demographic
de$Rwcd=de$Rwcd/mean(de$Rwcd,na.rm=T)#set mean weight == 1
summary(de$Rwcd)
max(de$Rwcd)/min(de$Rwcd)

de$Rwcp=de$Rwc*de$Rwp
de$Rwcp=de$Rwcp/mean(de$Rwcp,na.rm=T)#set mean weight == 1

de$Rwcdp=de$Rwc*de$Rwd*de$Rwp
de$Rwcdp=de$Rwcdp/mean(de$Rwcdp,na.rm=T)#set mean weight == 1

de=de[,-which(names(de) %in% c("Resp","Pop"))]#remove working columns


#CALCULATE Pl,Ll BY ELECTION:----
reg=de %>% group_by(Es) %>% summarise(mod = list(summary(clogit(Va ~ Pl+Ll +strata(Esalt), robust=T, weights=Rwcd, method="efron"))$coefficients[,1]))#create Pl and Ll by Es
length(unlist(reg))#366
Es=unlist(reg)[1:122]
Pl=unlist(reg)[123:366]
Pl=Pl[seq(1,244,2)]
Ll=unlist(reg)[123:366]
Ll=Ll[seq(2,244,2)]
ds=data.frame(Es,Pl,Ll)
ds$Pl=as.numeric(as.character(ds$Pl))
ds$Ll=as.numeric(as.character(ds$Ll))
ds$PlLl=ds$Pl-ds$Ll
ds$Ey=substr(ds$Es,5,8)
ds$Ey=as.numeric(ds$Ey)
ds$Ec=substr(ds$Es,1,3)#
ds$time=ds$Ey-1960
rm(reg,Es,Pl,Ll)

#merge Ed:
dss=de[,c(3,4)]
dss=dss %>% group_by(Es) %>% filter(row_number (Es) == 1)
ds=merge(ds,dss,by="Es")
ds$Ed=as.factor(ds$Ed)#(for graphs)

#merge NP:
dss=de[,c(3,7)]
dss=dss %>% group_by(Es) %>% filter(row_number (Es) == 1)
ds=merge(ds,dss,by="Es")
ds$NP=as.factor(ds$NP)#(for graphs)


#BASE FOR COUNTRY SLOPE FE:----
#Ec:Pl FE:
for (i1 in 1:length(sort(unique(de$Ec)))) {#creating dummy variables Ec:Pl (for FE)
  de[[paste0("Pl_",sort(unique(de$Ec))[i1])]]=ifelse(de$Ec==sort(unique(de$Ec))[i1],de$Pl,0)#de$Pl
}
#Ec:Ll FE:
for (i1 in 1:length(sort(unique(de$Ec)))) {#creating dummy variables Ec:Ll (for FE)
  de[[paste0("Ll_",sort(unique(de$Ec))[i1])]]=ifelse(de$Ec==sort(unique(de$Ec))[i1],de$Ll,0)#de$Ll
}
#create text for regression function:
txt=""
for (i1 in 1:length(sort(unique(de$Ec)))) {#creating function text for dummy variables Es:alt except for alt=1
  txt=paste(txt,paste0("Pl_",sort(unique(de$Ec))[i1]),sep="+")
  txt=paste(txt,paste0("Ll_",sort(unique(de$Ec))[i1]),sep="+")
}


#BASE FOR DENSITY PLOTS:----

#create dataset density all data:
dyALL = de[!is.na(de$Va)&!is.na(de$Pl)&!is.na(de$Ll),] %>% 
  group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dyALL$Eyn=dyALL$Eyn/nrow(de)#density by Ey
dyALL=dyALL %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey

#create dataset density data with left-right self reported position:
dyLRR = de[!is.na(de$LRR)&!is.na(de$Va)&!is.na(de$Pl)&!is.na(de$Ll),] %>% 
  group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dyLRR$Eyn=dyLRR$Eyn/nrow(de)#density by Ey
dyLRR=dyLRR %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey

#create dataset density data with left-right distance voter-party:
dyLRD = de[!is.na(de$LRD)&!is.na(de$Va)&!is.na(de$Pl)&!is.na(de$Ll),] %>% 
  group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dyLRD$Eyn=dyLRD$Eyn/nrow(de)#density by Ey
dyLRD=dyLRD %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey

#create dataset density data only countries observed before 1990:
dy90 = de[(de$Ec %in% c("CAN","DEU","DNK","ESP","GBR","GRC","ISL","NLD","NOR","SWE")),] %>% 
  group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dy90$Eyn=dy90$Eyn/nrow(de)#density by Ey
dy90=dy90 %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey


#SUMMARY STATISTICS:----

#list of lelections by country and data source:
table(de$Es,de$Ed)

#data:
length(unique(de$chid))#number of observations (individuals): 170048
length(unique(de$Esalt))#number of election-choices: 732
length(unique(de$Ec))#number of countries: 18
length(unique(de$Es))#number of elections: 122

#Pl,Ll:
median(de$Pl,na.rm = T)#median party likability chosen alterntive: 5
sd(de$Pl,na.rm = T)#standard deviation party likability chosen alterntive: 2.97
median(de$Ll,na.rm = T)#median leader likability chosen alterntive: 5
sd(de$Ll,na.rm = T)#standard deviation leader likability chosen alterntive: 2.95

median(de$Pl[de$Va==1],na.rm = T)#median party likability chosen alterntive: 8
sd(de$Pl[de$Va==1],na.rm = T)#standard deviation party likability chosen alterntive: 1.852487
median(de$Ll[de$Va==1],na.rm = T)#median leader likability chosen alterntive: 8
sd(de$Ll[de$Va==1],na.rm = T)#standard deviation leader likability chosen alterntive: 2.074604


##average Pl,Ll effect (with no time trend):
#Z-test functions:
Ztestfun_base=function(CLC){
  m=CLC$coefficients[paste0("Pl")]-CLC$coefficients[paste0("Ll")]#mean
  v=vcov(CLC)[paste0("Pl"),paste0("Pl")]+vcov(CLC)[paste0("Ll"),paste0("Ll")]-
    2*vcov(CLC)[paste0("Pl"),paste0("Ll")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
#regression:
CLC_base=clogit(Va ~ Pl+Ll
                +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                +strata(Esalt), robust=T, data=de, method="efron")#regression

#significance difference Pl-Ll coefficients:
Ztestfun_base(CLC_base)
#difference in odds between Pl and Ll:
summary(CLC_base)$coefficients[1,2]/summary(CLC_base)$coefficients[2,2]


##MEASURE LEADER LIKABILITY, PARTY LIKABILITY, PARTY IDENTIFICATION:----

#code Pi=0 and not NA in Es where it was asked:
de = de %>%
  mutate(Pi = as.numeric(as.character(Pi))) %>%
  group_by(Es) %>%
  mutate(Pit = ifelse(any(!is.na(Pi)), 1, 0))
de$Pi[de$Pit==1&is.na(de$Pi)]=0
de = de %>% select(-Pit)

#create dataset density data with Pi:
dyPi = de[!is.na(de$Va)&!is.na(de$Pl)&!is.na(de$Ll)&!is.na(de$Pi),] %>% 
  group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dyPi$Eyn=dyPi$Eyn/nrow(de)#density by Ey
dyPi=dyPi %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey


##Pl ON Ll:----
#confidence interval functions:
CI.Ll_Pl10fun=function(CLC,t0,pe){
  CI.Ll=matrix(NA,nrow=59,ncol=4)
  for (t in 1:59) {
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.Ll[t,1]=CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6#fitted values
    CI.Ll[t,2]=1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]+
      2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]+
      2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]+
      2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]+
      2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]#Variance
    CI.Ll[t,3]=CI.Ll[t,1]-qnorm(0.975)*sqrt(CI.Ll[t,2])#95% CI lower
    CI.Ll[t,4]=CI.Ll[t,1]+qnorm(0.975)*sqrt(CI.Ll[t,2])#95% CI upper
  }
  CI.Ll=as.data.frame(CI.Ll)
  CI.Ll$t=c(1961:2019)
  if (pe==1) {CI.Ll=CI.Ll[1:(10+t0),]}
  if (pe==2) {CI.Ll=CI.Ll[(11+t0):(20+t0),]}
  if (pe==3) {CI.Ll=CI.Ll[(21+t0):(30+t0),]}
  if (pe==4) {CI.Ll=CI.Ll[(31+t0):(40+t0),]}
  if (pe==5) {CI.Ll=CI.Ll[(41+t0):(50+t0),]}
  if (pe==6) {CI.Ll=CI.Ll[(51+t0):59,]}
  CI.Ll
}

#Z-test functions:
ZtestfunLl=function(CLC,per1,per2){
  m=CLC$coefficients[paste0("Ll:time",per2)]-CLC$coefficients[paste0("Ll:time",per1)]#mean
  v=vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]-
    vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regressions:
LMR_Ll_Pl_0=lm_robust(Pl ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
                      +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
                      , cluster=Ec, data=de, se_type="stata")#regression
LR_Ll_Pl_0=lm(Pl ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
                      +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
                      , data=de, se_type="stata")#regression


##Table 1## Party Likability on Leader Likability
#table (hypothesis testing):
LMR_Ll_Pl_0$r.squared
LMR_Ll_Pl_0$nobs
c(LMR_Ll_Pl_0$coefficients["Ll:time2"],LMR_Ll_Pl_0$std.error["Ll:time2"],LMR_Ll_Pl_0$p.value["Ll:time2"])#t2-t1=Ll:time2
ZtestfunLl(LMR_Ll_Pl_0,2,3)
ZtestfunLl(LMR_Ll_Pl_0,3,4)
ZtestfunLl(LMR_Ll_Pl_0,4,5)
ZtestfunLl(LMR_Ll_Pl_0,5,6)
#plots:
# tikz(paste0("plot_Pl_Ll_0.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Likability on Leader Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.66,0.84)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.84-0.66)+0.66))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_0,0,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_0,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_0,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_0,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_0,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_0,0,6)) )
# dev.off()


##Table D1##
##starting value for thresholds -4
#time variables:
t0=-4#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)
#regressions:
LMR_Ll_Pl_m4=lm_robust(Pl ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
                      +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
                      , cluster=Ec, data=de, se_type="stata")#regression
LR_Ll_Pl_m4=lm(Pl ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
              +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
              , data=de, se_type="stata")#regression

#table (hypothesis testing):
LMR_Ll_Pl_m4$r.squared
LMR_Ll_Pl_m4$nobs
c(LMR_Ll_Pl_m4$coefficients["Ll:time2"],LMR_Ll_Pl_m4$std.error["Ll:time2"],LMR_Ll_Pl_m4$p.value["Ll:time2"])#t2-t1=Ll:time2
ZtestfunLl(LMR_Ll_Pl_m4,2,3)
ZtestfunLl(LMR_Ll_Pl_m4,3,4)
ZtestfunLl(LMR_Ll_Pl_m4,4,5)
ZtestfunLl(LMR_Ll_Pl_m4,5,6)
#plots:
# tikz(paste0("plot_Pl_Ll_m4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Likability on Leader Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.66,0.84)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.84-0.66)+0.66))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_m4,-4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_m4,-4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_m4,-4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_m4,-4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_m4,-4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_m4,-4,6)) )
# dev.off()


##starting value for thresholds -2
#time variables:
t0=-2#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)
#regressions:
LMR_Ll_Pl_m2=lm_robust(Pl ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
                       +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
                       , cluster=Ec, data=de, se_type="stata")#regression
LR_Ll_Pl_m2=lm(Pl ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
               +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
               , data=de, se_type="stata")#regression
#table (hypothesis testing):
LMR_Ll_Pl_m2$r.squared
LMR_Ll_Pl_m2$nobs
c(LMR_Ll_Pl_m2$coefficients["Ll:time2"],LMR_Ll_Pl_m2$std.error["Ll:time2"],LMR_Ll_Pl_m2$p.value["Ll:time2"])#t2-t1=Ll:time2
ZtestfunLl(LMR_Ll_Pl_m2,2,3)
ZtestfunLl(LMR_Ll_Pl_m2,3,4)
ZtestfunLl(LMR_Ll_Pl_m2,4,5)
ZtestfunLl(LMR_Ll_Pl_m2,5,6)
#plots:
# tikz(paste0("plot_Pl_Ll_m2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Likability on Leader Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.66,0.84)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.84-0.66)+0.66))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_m2,-2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_m2,-2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_m2,-2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_m2,-2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_m2,-2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_m2,-2,6)) )
# dev.off()


##starting value for thresholds 2
#time variables:
t0=2#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)
#regressions:
LMR_Ll_Pl_2=lm_robust(Pl ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
                       +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
                       , cluster=Ec, data=de, se_type="stata")#regression
LR_Ll_Pl_2=lm(Pl ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
               +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
               , data=de, se_type="stata")#regression
#table (hypothesis testing):
LMR_Ll_Pl_2$r.squared
LMR_Ll_Pl_2$nobs
c(LMR_Ll_Pl_2$coefficients["Ll:time2"],LMR_Ll_Pl_2$std.error["Ll:time2"],LMR_Ll_Pl_2$p.value["Ll:time2"])#t2-t1=Ll:time2
ZtestfunLl(LMR_Ll_Pl_2,2,3)
ZtestfunLl(LMR_Ll_Pl_2,3,4)
ZtestfunLl(LMR_Ll_Pl_2,4,5)
ZtestfunLl(LMR_Ll_Pl_2,5,6)
#plots:
# tikz(paste0("plot_Pl_Ll_2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Likability on Leader Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.66,0.84)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.84-0.66)+0.66))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_2,2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_2,2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_2,2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_2,2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_2,2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_2,2,6)) )
# dev.off()


##starting value for thresholds 4
#time variables:
t0=4#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)
#regressions:
LMR_Ll_Pl_4=lm_robust(Pl ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
                       +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
                       , cluster=Ec, data=de, se_type="stata")#regression
LR_Ll_Pl_4=lm(Pl ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
               +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
               , data=de, se_type="stata")#regression
#table (hypothesis testing):
LMR_Ll_Pl_4$r.squared
LMR_Ll_Pl_4$nobs
c(LMR_Ll_Pl_4$coefficients["Ll:time2"],LMR_Ll_Pl_4$std.error["Ll:time2"],LMR_Ll_Pl_4$p.value["Ll:time2"])#t2-t1=Ll:time2
ZtestfunLl(LMR_Ll_Pl_4,2,3)
ZtestfunLl(LMR_Ll_Pl_4,3,4)
ZtestfunLl(LMR_Ll_Pl_4,4,5)
ZtestfunLl(LMR_Ll_Pl_4,5,6)
#plots:
# tikz(paste0("plot_Pl_Ll_4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Likability on Leader Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.66,0.84)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.84-0.66)+0.66))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_4,4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_4,4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_4,4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_4,4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_4,4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Ll_Pl_4,4,6)) )
# dev.off()


##Table I4##
##table full results:
stargazer(LR_Ll_Pl_m4,LR_Ll_Pl_m2,LR_Ll_Pl_0,LR_Ll_Pl_2,LR_Ll_Pl_4,
          se = list(LMR_Ll_Pl_m4$std.error,LMR_Ll_Pl_m2$std.error,LMR_Ll_Pl_0$std.error,LMR_Ll_Pl_2$std.error,LMR_Ll_Pl_4$std.error))



##Pi ON Pl:----
#confidence interval functions:
CI.Pl_Pl10fun=function(CLC,t0,pe){
  CI.Pl=matrix(NA,nrow=59,ncol=4)
  for (t in 1:59) {
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.Pl[t,1]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6#fitted values
    CI.Pl[t,2]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]+2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]+
      2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]+2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]+
      2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]+
      2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]#Variance
    CI.Pl[t,3]=CI.Pl[t,1]-qnorm(0.975)*sqrt(CI.Pl[t,2])#95% CI lower
    CI.Pl[t,4]=CI.Pl[t,1]+qnorm(0.975)*sqrt(CI.Pl[t,2])#95% CI upper
  }
  CI.Pl=as.data.frame(CI.Pl)
  CI.Pl$t=c(1961:2019)
  if (pe==1) {CI.Pl=CI.Pl[1:(10+t0),]}
  if (pe==2) {CI.Pl=CI.Pl[(11+t0):(20+t0),]}
  if (pe==3) {CI.Pl=CI.Pl[(21+t0):(30+t0),]}
  if (pe==4) {CI.Pl=CI.Pl[(31+t0):(40+t0),]}
  if (pe==5) {CI.Pl=CI.Pl[(41+t0):(50+t0),]}
  if (pe==6) {CI.Pl=CI.Pl[(51+t0):59,]}
  CI.Pl
}

#Z-test functions:
ZtestfunPl=function(CLC,per1,per2){
  m=CLC$coefficients[paste0("Pl:time",per2)]-CLC$coefficients[paste0("Pl:time",per1)]#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]-
    vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regressions:
LMR_Pi_Pl_0=lm_robust(Pi ~ Pl:time2+Pl:time3+Pl:time4+Pl:time5+Pl:time6 +I(Ec)
                      +Pl_AUS+Pl_AUT+Pl_CAN+Pl_DEU+Pl_DNK+Pl_ESP+Pl_FIN+Pl_GBR+Pl_GRC+Pl_IRL+Pl_ISL+Pl_ISR+Pl_ITA+Pl_NLD+Pl_NOR+Pl_NZL+Pl_PRT+Pl_SWE-1
                      , cluster=Ec, data=de, se_type="stata")#regression
LR_Pi_Pl_0=lm(Pi ~ Pl:time2+Pl:time3+Pl:time4+Pl:time5+Pl:time6 +I(Ec)
                      +Pl_AUS+Pl_AUT+Pl_CAN+Pl_DEU+Pl_DNK+Pl_ESP+Pl_FIN+Pl_GBR+Pl_GRC+Pl_IRL+Pl_ISL+Pl_ISR+Pl_ITA+Pl_NLD+Pl_NOR+Pl_NZL+Pl_PRT+Pl_SWE-1
                      , data=de, se_type="stata")#regression


##Table 1## Party Identification on Party Likability
#table (hypothesis testing):
LMR_Pi_Pl_0$r.squared
LMR_Pi_Pl_0$nobs
c(LMR_Pi_Pl_0$coefficients["Pl:time2"],LMR_Pi_Pl_0$std.error["Pl:time2"],LMR_Pi_Pl_0$p.value["Pl:time2"])#t2-t1=Pl:time2
ZtestfunPl(LMR_Pi_Pl_0,2,3)
ZtestfunPl(LMR_Pi_Pl_0,3,4)
ZtestfunPl(LMR_Pi_Pl_0,4,5)
ZtestfunPl(LMR_Pi_Pl_0,5,6)
#plots:
# tikz(paste0("plot_Pi_Pl_0.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Identif. on Party Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.03,0.07)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.07-0.03)+0.03))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyPi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_0,0,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_0,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_0,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_0,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_0,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_0,0,6)) )
# dev.off()


##Table D2##
##starting value for thresholds -4
#time variables:
t0=-4#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)
#regressions:
LMR_Pi_Pl_m4=lm_robust(Pi ~ Pl:time2+Pl:time3+Pl:time4+Pl:time5+Pl:time6 +I(Ec)
                      +Pl_AUS+Pl_AUT+Pl_CAN+Pl_DEU+Pl_DNK+Pl_ESP+Pl_FIN+Pl_GBR+Pl_GRC+Pl_IRL+Pl_ISL+Pl_ISR+Pl_ITA+Pl_NLD+Pl_NOR+Pl_NZL+Pl_PRT+Pl_SWE-1
                      , cluster=Ec, data=de, se_type="stata")#regression
LR_Pi_Pl_m4=lm(Pi ~ Pl:time2+Pl:time3+Pl:time4+Pl:time5+Pl:time6 +I(Ec)
              +Pl_AUS+Pl_AUT+Pl_CAN+Pl_DEU+Pl_DNK+Pl_ESP+Pl_FIN+Pl_GBR+Pl_GRC+Pl_IRL+Pl_ISL+Pl_ISR+Pl_ITA+Pl_NLD+Pl_NOR+Pl_NZL+Pl_PRT+Pl_SWE-1
              , data=de, se_type="stata")#regression
#table (hypothesis testing):
LMR_Pi_Pl_m4$r.squared
LMR_Pi_Pl_m4$nobs
c(LMR_Pi_Pl_m4$coefficients["Pl:time2"],LMR_Pi_Pl_m4$std.error["Pl:time2"],LMR_Pi_Pl_m4$p.value["Pl:time2"])#t2-t1=Pl:time2
ZtestfunPl(LMR_Pi_Pl_m4,2,3)
ZtestfunPl(LMR_Pi_Pl_m4,3,4)
ZtestfunPl(LMR_Pi_Pl_m4,4,5)
ZtestfunPl(LMR_Pi_Pl_m4,5,6)
#plots:
# tikz(paste0("plot_Pi_Pl_m4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Identif. on Party Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.03,0.07)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.07-0.03)+0.03))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyPi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_m4,-4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_m4,-4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_m4,-4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_m4,-4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_m4,-4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_m4,-4,6)) )
# dev.off()


##starting value for thresholds -2
#time variables:
t0=-2#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)
#regressions:
LMR_Pi_Pl_m2=lm_robust(Pi ~ Pl:time2+Pl:time3+Pl:time4+Pl:time5+Pl:time6 +I(Ec)
                       +Pl_AUS+Pl_AUT+Pl_CAN+Pl_DEU+Pl_DNK+Pl_ESP+Pl_FIN+Pl_GBR+Pl_GRC+Pl_IRL+Pl_ISL+Pl_ISR+Pl_ITA+Pl_NLD+Pl_NOR+Pl_NZL+Pl_PRT+Pl_SWE-1
                       , cluster=Ec, data=de, se_type="stata")#regression
LR_Pi_Pl_m2=lm(Pi ~ Pl:time2+Pl:time3+Pl:time4+Pl:time5+Pl:time6 +I(Ec)
               +Pl_AUS+Pl_AUT+Pl_CAN+Pl_DEU+Pl_DNK+Pl_ESP+Pl_FIN+Pl_GBR+Pl_GRC+Pl_IRL+Pl_ISL+Pl_ISR+Pl_ITA+Pl_NLD+Pl_NOR+Pl_NZL+Pl_PRT+Pl_SWE-1
               , data=de, se_type="stata")#regression
#table (hypothesis testing):
LMR_Pi_Pl_m2$r.squared
LMR_Pi_Pl_m2$nobs
c(LMR_Pi_Pl_m2$coefficients["Pl:time2"],LMR_Pi_Pl_m2$std.error["Pl:time2"],LMR_Pi_Pl_m2$p.value["Pl:time2"])#t2-t1=Pl:time2
ZtestfunPl(LMR_Pi_Pl_m2,2,3)
ZtestfunPl(LMR_Pi_Pl_m2,3,4)
ZtestfunPl(LMR_Pi_Pl_m2,4,5)
ZtestfunPl(LMR_Pi_Pl_m2,5,6)
#plots:
# tikz(paste0("plot_Pi_Pl_m2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Identif. on Party Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.03,0.07)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.07-0.03)+0.03))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyPi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_m2,-2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_m2,-2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_m2,-2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_m2,-2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_m2,-2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_m2,-2,6)) )
# dev.off()


##starting value for thresholds 2
#time variables:
t0=2#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)
#regressions:
LMR_Pi_Pl_2=lm_robust(Pi ~ Pl:time2+Pl:time3+Pl:time4+Pl:time5+Pl:time6 +I(Ec)
                       +Pl_AUS+Pl_AUT+Pl_CAN+Pl_DEU+Pl_DNK+Pl_ESP+Pl_FIN+Pl_GBR+Pl_GRC+Pl_IRL+Pl_ISL+Pl_ISR+Pl_ITA+Pl_NLD+Pl_NOR+Pl_NZL+Pl_PRT+Pl_SWE-1
                       , cluster=Ec, data=de, se_type="stata")#regression
LR_Pi_Pl_2=lm(Pi ~ Pl:time2+Pl:time3+Pl:time4+Pl:time5+Pl:time6 +I(Ec)
               +Pl_AUS+Pl_AUT+Pl_CAN+Pl_DEU+Pl_DNK+Pl_ESP+Pl_FIN+Pl_GBR+Pl_GRC+Pl_IRL+Pl_ISL+Pl_ISR+Pl_ITA+Pl_NLD+Pl_NOR+Pl_NZL+Pl_PRT+Pl_SWE-1
               , data=de, se_type="stata")#regression
#table (hypothesis testing):
LMR_Pi_Pl_2$r.squared
LMR_Pi_Pl_2$nobs
c(LMR_Pi_Pl_2$coefficients["Pl:time2"],LMR_Pi_Pl_2$std.error["Pl:time2"],LMR_Pi_Pl_2$p.value["Pl:time2"])#t2-t1=Pl:time2
ZtestfunPl(LMR_Pi_Pl_2,2,3)
ZtestfunPl(LMR_Pi_Pl_2,3,4)
ZtestfunPl(LMR_Pi_Pl_2,4,5)
ZtestfunPl(LMR_Pi_Pl_2,5,6)
#plots:
# tikz(paste0("plot_Pi_Pl_2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Identif. on Party Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.03,0.07)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.07-0.03)+0.03))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyPi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_2,2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_2,2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_2,2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_2,2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_2,2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_2,2,6)) )
# dev.off()


##starting value for thresholds -4
#time variables:
t0=4#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)
#regressions:
LMR_Pi_Pl_4=lm_robust(Pi ~ Pl:time2+Pl:time3+Pl:time4+Pl:time5+Pl:time6 +I(Ec)
                       +Pl_AUS+Pl_AUT+Pl_CAN+Pl_DEU+Pl_DNK+Pl_ESP+Pl_FIN+Pl_GBR+Pl_GRC+Pl_IRL+Pl_ISL+Pl_ISR+Pl_ITA+Pl_NLD+Pl_NOR+Pl_NZL+Pl_PRT+Pl_SWE-1
                       , cluster=Ec, data=de, se_type="stata")#regression
LR_Pi_Pl_4=lm(Pi ~ Pl:time2+Pl:time3+Pl:time4+Pl:time5+Pl:time6 +I(Ec)
               +Pl_AUS+Pl_AUT+Pl_CAN+Pl_DEU+Pl_DNK+Pl_ESP+Pl_FIN+Pl_GBR+Pl_GRC+Pl_IRL+Pl_ISL+Pl_ISR+Pl_ITA+Pl_NLD+Pl_NOR+Pl_NZL+Pl_PRT+Pl_SWE-1
               , data=de, se_type="stata")#regression
#table (hypothesis testing):
LMR_Pi_Pl_4$r.squared
LMR_Pi_Pl_4$nobs
c(LMR_Pi_Pl_4$coefficients["Pl:time2"],LMR_Pi_Pl_4$std.error["Pl:time2"],LMR_Pi_Pl_4$p.value["Pl:time2"])#t2-t1=Pl:time2
ZtestfunPl(LMR_Pi_Pl_4,2,3)
ZtestfunPl(LMR_Pi_Pl_4,3,4)
ZtestfunPl(LMR_Pi_Pl_4,4,5)
ZtestfunPl(LMR_Pi_Pl_4,5,6)
#plots:
# tikz(paste0("plot_Pi_Pl_4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Identif. on Party Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.03,0.07)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.07-0.03)+0.03))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyPi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_4,4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_4,4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_4,4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_4,4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_4,4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pl_Pl10fun(LMR_Pi_Pl_4,4,6)) )
# dev.off()


##Table I4##
##table full results:
stargazer(LR_Pi_Pl_m4,LR_Pi_Pl_m2,LR_Pi_Pl_0,LR_Pi_Pl_2,LR_Pi_Pl_4,
          se = list(LMR_Pi_Pl_m4$std.error,LMR_Pi_Pl_m2$std.error,LMR_Pi_Pl_0$std.error,LMR_Pi_Pl_2$std.error,LMR_Pi_Pl_4$std.error))


##Pi ON Ll AND TABLE RESULTS:----

#confidence interval functions:
CI.Ll_Pl10fun=function(CLC,t0,pe){
  CI.Ll=matrix(NA,nrow=59,ncol=4)
  for (t in 1:59) {
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.Ll[t,1]=CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6#fitted values
    CI.Ll[t,2]=1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]+
      2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]+
      2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]+
      2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]+
      2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]#Variance
    CI.Ll[t,3]=CI.Ll[t,1]-qnorm(0.975)*sqrt(CI.Ll[t,2])#95% CI lower
    CI.Ll[t,4]=CI.Ll[t,1]+qnorm(0.975)*sqrt(CI.Ll[t,2])#95% CI upper
  }
  CI.Ll=as.data.frame(CI.Ll)
  CI.Ll$t=c(1961:2019)
  if (pe==1) {CI.Ll=CI.Ll[1:(10+t0),]}
  if (pe==2) {CI.Ll=CI.Ll[(11+t0):(20+t0),]}
  if (pe==3) {CI.Ll=CI.Ll[(21+t0):(30+t0),]}
  if (pe==4) {CI.Ll=CI.Ll[(31+t0):(40+t0),]}
  if (pe==5) {CI.Ll=CI.Ll[(41+t0):(50+t0),]}
  if (pe==6) {CI.Ll=CI.Ll[(51+t0):59,]}
  CI.Ll
}

#Z-test functions:
ZtestfunLl=function(CLC,per1,per2){
  m=CLC$coefficients[paste0("Ll:time",per2)]-CLC$coefficients[paste0("Ll:time",per1)]#mean
  v=vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]-
    vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regressions:
LMR_Pi_Ll_0=lm_robust(Pi ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
                      +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
                      , cluster=Ec, data=de, se_type="stata")#regression
LR_Pi_Ll_0=lm(Pi ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
                      +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
                      , data=de, se_type="stata")#regression


##Table 1## Party Identification on Leader Likability
#table (hypothesis testing):
LMR_Pi_Ll_0$r.squared
LMR_Pi_Ll_0$nobs
c(LMR_Pi_Ll_0$coefficients["Ll:time2"],LMR_Pi_Ll_0$std.error["Ll:time2"],LMR_Pi_Ll_0$p.value["Ll:time2"])#t2-t1=Ll:time2
ZtestfunLl(LMR_Pi_Ll_0,2,3)
ZtestfunLl(LMR_Pi_Ll_0,3,4)
ZtestfunLl(LMR_Pi_Ll_0,4,5)
ZtestfunLl(LMR_Pi_Ll_0,5,6)
#plots:
# tikz(paste0("plot_Pi_Ll_0.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Identif. on Leader Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.03,0.07)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.07-0.03)+0.03))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyPi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_0,0,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_0,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_0,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_0,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_0,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_0,0,6)) )
# dev.off()


##Table D3##
##table full results:
stargazer(LR_Ll_Pl_0,LR_Pi_Pl_0,LR_Pi_Ll_0,
          se = list(LMR_Ll_Pl_0$std.error,LMR_Pi_Pl_0$std.error,LMR_Pi_Ll_0$std.error))


##starting value for thresholds -4
#time variables:
t0=-4#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)
#regressions:
LMR_Pi_Ll_m4=lm_robust(Pi ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
                      +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
                      , cluster=Ec, data=de, se_type="stata")#regression
LR_Pi_Ll_m4=lm(Pi ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
              +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
              , data=de, se_type="stata")#regression
#table (hypothesis testing):
LMR_Pi_Ll_m4$r.squared
LMR_Pi_Ll_m4$nobs
c(LMR_Pi_Ll_m4$coefficients["Ll:time2"],LMR_Pi_Ll_m4$std.error["Ll:time2"],LMR_Pi_Ll_m4$p.value["Ll:time2"])#t2-t1=Ll:time2
ZtestfunLl(LMR_Pi_Ll_m4,2,3)
ZtestfunLl(LMR_Pi_Ll_m4,3,4)
ZtestfunLl(LMR_Pi_Ll_m4,4,5)
ZtestfunLl(LMR_Pi_Ll_m4,5,6)
#plots:
# tikz(paste0("plot_Pi_Ll_m4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Identif. on Leader Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.03,0.07)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.07-0.03)+0.03))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyPi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_m4,-4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_m4,-4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_m4,-4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_m4,-4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_m4,-4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_m4,-4,6)) )
# dev.off()


##starting value for thresholds -2
#time variables:
t0=-2#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)
#regressions:
LMR_Pi_Ll_m2=lm_robust(Pi ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
                       +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
                       , cluster=Ec, data=de, se_type="stata")#regression
LR_Pi_Ll_m2=lm(Pi ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
               +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
               , data=de, se_type="stata")#regression
#table (hypothesis testing):
LMR_Pi_Ll_m2$r.squared
LMR_Pi_Ll_m2$nobs
c(LMR_Pi_Ll_m2$coefficients["Ll:time2"],LMR_Pi_Ll_m2$std.error["Ll:time2"],LMR_Pi_Ll_m2$p.value["Ll:time2"])#t2-t1=Ll:time2
ZtestfunLl(LMR_Pi_Ll_m2,2,3)
ZtestfunLl(LMR_Pi_Ll_m2,3,4)
ZtestfunLl(LMR_Pi_Ll_m2,4,5)
ZtestfunLl(LMR_Pi_Ll_m2,5,6)
#plots:
# tikz(paste0("plot_Pi_Ll_m2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Identif. on Leader Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.03,0.07)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.07-0.03)+0.03))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyPi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_m2,-2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_m2,-2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_m2,-2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_m2,-2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_m2,-2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_m2,-2,6)) )
# dev.off()


##starting value for thresholds 2
#time variables:
t0=2#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)
#regressions:
LMR_Pi_Ll_2=lm_robust(Pi ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
                       +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
                       , cluster=Ec, data=de, se_type="stata")#regression
LR_Pi_Ll_2=lm(Pi ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
               +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
               , data=de, se_type="stata")#regression
#table (hypothesis testing):
LMR_Pi_Ll_2$r.squared
LMR_Pi_Ll_2$nobs
c(LMR_Pi_Ll_2$coefficients["Ll:time2"],LMR_Pi_Ll_2$std.error["Ll:time2"],LMR_Pi_Ll_2$p.value["Ll:time2"])#t2-t1=Ll:time2
ZtestfunLl(LMR_Pi_Ll_2,2,3)
ZtestfunLl(LMR_Pi_Ll_2,3,4)
ZtestfunLl(LMR_Pi_Ll_2,4,5)
ZtestfunLl(LMR_Pi_Ll_2,5,6)
#plots:
# tikz(paste0("plot_Pi_Ll_2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Identif. on Leader Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.03,0.07)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.07-0.03)+0.03))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyPi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_2,2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_2,2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_2,2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_2,2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_2,2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_2,2,6)) )
# dev.off()


##starting value for thresholds 4
#time variables:
t0=4#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)
#regressions:
LMR_Pi_Ll_4=lm_robust(Pi ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
                       +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
                       , cluster=Ec, data=de, se_type="stata")#regression
LR_Pi_Ll_4=lm(Pi ~ Ll:time2+Ll:time3+Ll:time4+Ll:time5+Ll:time6 +I(Ec)
               +Ll_AUS+Ll_AUT+Ll_CAN+Ll_DEU+Ll_DNK+Ll_ESP+Ll_FIN+Ll_GBR+Ll_GRC+Ll_IRL+Ll_ISL+Ll_ISR+Ll_ITA+Ll_NLD+Ll_NOR+Ll_NZL+Ll_PRT+Ll_SWE-1
               , data=de, se_type="stata")#regression
#table (hypothesis testing):
LMR_Pi_Ll_4$r.squared
LMR_Pi_Ll_4$nobs
c(LMR_Pi_Ll_4$coefficients["Ll:time2"],LMR_Pi_Ll_4$std.error["Ll:time2"],LMR_Pi_Ll_4$p.value["Ll:time2"])#t2-t1=Ll:time2
ZtestfunLl(LMR_Pi_Ll_4,2,3)
ZtestfunLl(LMR_Pi_Ll_4,3,4)
ZtestfunLl(LMR_Pi_Ll_4,4,5)
ZtestfunLl(LMR_Pi_Ll_4,5,6)
#plots:
# tikz(paste0("plot_Pi_Ll_4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Party Identif. on Leader Likability") +xlab("Year") +ylab("estimated coefficient") + 
        coord_cartesian(ylim=c(0.03,0.07)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.07-0.03)+0.03))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyPi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_4,4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_4,4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_4,4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_4,4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_4,4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Ll_Pl10fun(LMR_Pi_Ll_4,4,6)) )
# dev.off()


##Table I5##
##table full results:
stargazer(LR_Pi_Ll_m4,LR_Pi_Ll_m2,LR_Pi_Ll_0,LR_Pi_Ll_2,LR_Pi_Ll_4,
          se = list(LMR_Pi_Ll_m4$std.error,LMR_Pi_Ll_m2$std.error,LMR_Pi_Ll_0$std.error,LMR_Pi_Ll_2$std.error,LMR_Pi_Ll_4$std.error))



#ANALYSIS 10Y SEGMENTS:----

#confidence interval functions:
CI.PlLllses10funCTY=function(CLC,t0,pe,cty){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    # t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.PlLl[t,1]=CLC$coefficients[paste0("Pl_",cty)]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6-
      (CLC$coefficients[paste0("Ll_",cty)]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)[paste0("Pl_",cty),paste0("Pl_",cty)]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      1*vcov(CLC)[paste0("Ll_",cty),paste0("Ll_",cty)]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]-
      2*1*1*vcov(CLC)[paste0("Pl_",cty),paste0("Ll_",cty)]+2*1*t2*vcov(CLC)[paste0("Pl_",cty),"Pl:time2"]-2*1*t2*vcov(CLC)[paste0("Pl_",cty),"Ll:time2"]+2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Pl:time3"]-2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Ll:time3"]+
      2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Pl:time4"]-2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Ll:time4"]+2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Pl:time5"]-2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Ll:time5"]+2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Pl:time6"]-2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Ll:time6"]-
      2*1*t2*vcov(CLC)[paste0("Ll_",cty),"Pl:time2"]+2*1*t2*vcov(CLC)[paste0("Ll_",cty),"Ll:time2"]-2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Pl:time3"]+2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Ll:time3"]-
      2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Pl:time4"]+2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Ll:time4"]-2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Pl:time5"]+2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Ll:time5"]-2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Pl:time6"]+2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Ll:time6"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}
CI.Pllses10fun=function(CLC,t0,pe){ #function creating Confidence Interval for Pl
  CI.Pl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    # t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.Pl[t,1]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6#fitted values
    CI.Pl[t,2]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]+2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]+
      2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]+2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]+
      2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]+
      2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]#Variance
    CI.Pl[t,3]=CI.Pl[t,1]-qnorm(0.975)*sqrt(CI.Pl[t,2])#95% CI lower
    CI.Pl[t,4]=CI.Pl[t,1]+qnorm(0.975)*sqrt(CI.Pl[t,2])#95% CI upper
  }
  CI.Pl=as.data.frame(CI.Pl)
  CI.Pl$t=c(1961:2019)
  if (pe==1) {CI.Pl=CI.Pl[1:(10+t0),]}
  if (pe==2) {CI.Pl=CI.Pl[(11+t0):(20+t0),]}
  if (pe==3) {CI.Pl=CI.Pl[(21+t0):(30+t0),]}
  if (pe==4) {CI.Pl=CI.Pl[(31+t0):(40+t0),]}
  if (pe==5) {CI.Pl=CI.Pl[(41+t0):(50+t0),]}
  if (pe==6) {CI.Pl=CI.Pl[(51+t0):59,]}
  CI.Pl
}
CI.Lllses10fun=function(CLC,t0,pe){ #function creating Confidence Interval for Ll
  CI.Ll=matrix(NA,nrow=59,ncol=4)#Ll:
  for (t in 1:59) {
    # t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.Ll[t,1]=CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6#fitted values
    CI.Ll[t,2]=1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]+
      2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]+
      2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]+
      2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]+
      2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]#Variance
    CI.Ll[t,3]=CI.Ll[t,1]-qnorm(0.975)*sqrt(CI.Ll[t,2])#95% CI lower
    CI.Ll[t,4]=CI.Ll[t,1]+qnorm(0.975)*sqrt(CI.Ll[t,2])#95% CI upper
  }
  CI.Ll=as.data.frame(CI.Ll)
  CI.Ll$t=c(1961:2019)
  if (pe==1) {CI.Ll=CI.Ll[1:(10+t0),]}
  if (pe==2) {CI.Ll=CI.Ll[(11+t0):(20+t0),]}
  if (pe==3) {CI.Ll=CI.Ll[(21+t0):(30+t0),]}
  if (pe==4) {CI.Ll=CI.Ll[(31+t0):(40+t0),]}
  if (pe==5) {CI.Ll=CI.Ll[(41+t0):(50+t0),]}
  if (pe==6) {CI.Ll=CI.Ll[(51+t0):59,]}
  CI.Ll
}

#Z-test functions:
ZtestfunT12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunT16=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",6)]-CLC$coefficients[paste0("Ll:time",6)]#mean
  v=vcov(CLC)[paste0("Pl:time",6),paste0("Pl:time",6)]+vcov(CLC)[paste0("Ll:time",6),paste0("Ll:time",6)]-
    6*vcov(CLC)[paste0("Pl:time",6),paste0("Ll:time",6)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunT=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

CLC_lses10_0=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                      +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                    +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses10_0$coefficients)[names(CLC_lses10_0$coefficients)=="time2:Ll"]="Ll:time2"


##Table 2##
#table (hypothesis testing):
stargazer(CLC_lses10_0,no.space = T,single.row = T)
ZtestfunT12(CLC_lses10_0)
ZtestfunT(CLC_lses10_0,2,3)
ZtestfunT(CLC_lses10_0,3,4)
ZtestfunT(CLC_lses10_0,4,5)
ZtestfunT(CLC_lses10_0,5,6)
#difference between 60s and 2010s:
ZtestfunT16(CLC_lses10_0)
#difference between 80s and 2010s:
ZtestfunT(CLC_lses10_0,3,6)
#list and test countries by average variance:
vaCLC_lses10_0=matrix(NA,nrow=length(sort(unique(de$Ec))),ncol=2)#average variance by country over time
for(i in 1:length(sort(unique(de$Ec)))) {#get average variance over time
  vaCLC_lses10_0[i,1]=paste0(sort(unique(de$Ec))[i])
  vaCLC_lses10_0[i,2]=mean(CI.PlLllses10funCTY(CLC_lses10_0,0,4,sort(unique(de$Ec))[i])[,2],na.rm=T)
}
vaCLC_lses10_0=as.data.frame(vaCLC_lses10_0)
vaCLC_lses10_0=vaCLC_lses10_0[order(vaCLC_lses10_0$V2),]
vaCLC_lses10_0[9,1]#country with median variance (the same considering each period)
vaCLC_lses10_0[,1]#list of countries by variance (small to high)#virtually the same than two periods
#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_0","_PlLl.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_0,0,1,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_0,0,2,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_0,0,3,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_0,0,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_0,0,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_0,0,6,sort(unique(de$Ec))[11])) )
# dev.off()


##Figure 3##
##plots only Pl,Ll:
# tikz(paste0("plot_","CLC_lses10","_Pl_0.tex"),width=4, height=3)#plot Pl:
plot( ggplot()
      +ggtitle("")
      +xlab("Year") +ylab("Party Effect") + coord_cartesian(ylim=c(0.5001,0.7)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.7-0.5001)+0.5001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_0,0,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_0,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_0,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_0,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_0,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_0,0,6)) )
# dev.off()
# tikz(paste0("plot_","CLC_lses10","_Ll_0.tex"),width=4, height=3)#plot Ll:
plot( ggplot()
      +ggtitle("")
      +xlab("Year") +ylab("Leader Effect") + coord_cartesian(ylim=c(0.0001,0.2)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.2-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_0,0,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_0,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_0,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_0,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_0,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_0,0,6)) )
# dev.off()


##ANALYSIS 10Y SEGMENTS - ALTERNATIVE THRESHOLDS AND FULL REUSLTS TABLE:----

#confidence interval functions:
CI.PlLllses10funCTY=function(CLC,t0,pe,cty){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    # t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.PlLl[t,1]=CLC$coefficients[paste0("Pl_",cty)]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6-
      (CLC$coefficients[paste0("Ll_",cty)]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)[paste0("Pl_",cty),paste0("Pl_",cty)]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      1*vcov(CLC)[paste0("Ll_",cty),paste0("Ll_",cty)]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]-
      2*1*1*vcov(CLC)[paste0("Pl_",cty),paste0("Ll_",cty)]+2*1*t2*vcov(CLC)[paste0("Pl_",cty),"Pl:time2"]-2*1*t2*vcov(CLC)[paste0("Pl_",cty),"Ll:time2"]+2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Pl:time3"]-2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Ll:time3"]+
      2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Pl:time4"]-2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Ll:time4"]+2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Pl:time5"]-2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Ll:time5"]+2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Pl:time6"]-2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Ll:time6"]-
      2*1*t2*vcov(CLC)[paste0("Ll_",cty),"Pl:time2"]+2*1*t2*vcov(CLC)[paste0("Ll_",cty),"Ll:time2"]-2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Pl:time3"]+2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Ll:time3"]-
      2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Pl:time4"]+2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Ll:time4"]-2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Pl:time5"]+2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Ll:time5"]-2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Pl:time6"]+2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Ll:time6"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}
CI.Pllses10fun=function(CLC,t0,pe){ #function creating Confidence Interval for Pl
  CI.Pl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    # t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.Pl[t,1]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6#fitted values
    CI.Pl[t,2]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]+2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]+
      2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]+2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]+
      2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]+
      2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]#Variance
    CI.Pl[t,3]=CI.Pl[t,1]-qnorm(0.975)*sqrt(CI.Pl[t,2])#95% CI lower
    CI.Pl[t,4]=CI.Pl[t,1]+qnorm(0.975)*sqrt(CI.Pl[t,2])#95% CI upper
  }
  CI.Pl=as.data.frame(CI.Pl)
  CI.Pl$t=c(1961:2019)
  if (pe==1) {CI.Pl=CI.Pl[1:(10+t0),]}
  if (pe==2) {CI.Pl=CI.Pl[(11+t0):(20+t0),]}
  if (pe==3) {CI.Pl=CI.Pl[(21+t0):(30+t0),]}
  if (pe==4) {CI.Pl=CI.Pl[(31+t0):(40+t0),]}
  if (pe==5) {CI.Pl=CI.Pl[(41+t0):(50+t0),]}
  if (pe==6) {CI.Pl=CI.Pl[(51+t0):59,]}
  CI.Pl
}
CI.Lllses10fun=function(CLC,t0,pe){ #function creating Confidence Interval for Ll
  CI.Ll=matrix(NA,nrow=59,ncol=4)#Ll:
  for (t in 1:59) {
    # t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.Ll[t,1]=CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6#fitted values
    CI.Ll[t,2]=1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]+
      2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]+
      2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]+
      2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]+
      2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]#Variance
    CI.Ll[t,3]=CI.Ll[t,1]-qnorm(0.975)*sqrt(CI.Ll[t,2])#95% CI lower
    CI.Ll[t,4]=CI.Ll[t,1]+qnorm(0.975)*sqrt(CI.Ll[t,2])#95% CI upper
  }
  CI.Ll=as.data.frame(CI.Ll)
  CI.Ll$t=c(1961:2019)
  if (pe==1) {CI.Ll=CI.Ll[1:(10+t0),]}
  if (pe==2) {CI.Ll=CI.Ll[(11+t0):(20+t0),]}
  if (pe==3) {CI.Ll=CI.Ll[(21+t0):(30+t0),]}
  if (pe==4) {CI.Ll=CI.Ll[(31+t0):(40+t0),]}
  if (pe==5) {CI.Ll=CI.Ll[(41+t0):(50+t0),]}
  if (pe==6) {CI.Ll=CI.Ll[(51+t0):59,]}
  CI.Ll
}

#Z-test functions:
ZtestfunT12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunT=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}


##Table D4## and ##Figure D1##
##starting value for thresholds -4
#time variables:
t0=-4#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

CLC_lses10_m4=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                      +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                    +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses10_m4$coefficients)[names(CLC_lses10_m4$coefficients)=="time2:Ll"]="Ll:time2"

#table (hypothesis testing):
stargazer(CLC_lses10_m4,no.space = T,single.row = T)
ZtestfunT12(CLC_lses10_m4)
ZtestfunT(CLC_lses10_m4,2,3)
ZtestfunT(CLC_lses10_m4,3,4)
ZtestfunT(CLC_lses10_m4,4,5)
ZtestfunT(CLC_lses10_m4,5,6)

#plots (median cty):
# tikz(paste0("plot_CLC_lses10_0_PlLl_m4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("thresholds offset -4") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_m4,-4,1,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_m4,-4,2,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_m4,-4,3,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_m4,-4,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_m4,-4,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_m4,-4,6,sort(unique(de$Ec))[11])) )
# dev.off()

##plots only Pl,Ll:
# tikz(paste0("plot_","CLC_lses10","_Pl_m4.tex"),width=4, height=3)#plot Pl:
plot( ggplot()
      +ggtitle("")
      +xlab("Year") +ylab("Party Effect") + coord_cartesian(ylim=c(0.5001,0.7)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.7-0.5001)+0.5001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_m4,-4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_m4,-4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_m4,-4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_m4,-4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_m4,-4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_m4,-4,6)) )
# dev.off()
# tikz(paste0("plot_","CLC_lses10","_Ll_m4.tex"),width=4, height=3)#plot Ll:
plot( ggplot()
      +ggtitle("")
      +xlab("Year") +ylab("Leader Effect") + coord_cartesian(ylim=c(0.0001,0.2)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.2-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_m4,-4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_m4,-4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_m4,-4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_m4,-4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_m4,-4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_m4,-4,6)) )
# dev.off()


##starting value for thresholds -2
#time variables:
t0=-2#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

CLC_lses10_m2=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                       +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                     +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses10_m2$coefficients)[names(CLC_lses10_m2$coefficients)=="time2:Ll"]="Ll:time2"

#table (hypothesis testing):
stargazer(CLC_lses10_m2,no.space = T,single.row = T)
ZtestfunT12(CLC_lses10_m2)
ZtestfunT(CLC_lses10_m2,2,3)
ZtestfunT(CLC_lses10_m2,3,4)
ZtestfunT(CLC_lses10_m2,4,5)
ZtestfunT(CLC_lses10_m2,5,6)

#plots (median cty):
# tikz(paste0("plot_CLC_lses10_0_PlLl_m2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("thresholds offset -2") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_m2,-2,1,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_m2,-2,2,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_m2,-2,3,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_m2,-2,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_m2,-2,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_m2,-2,6,sort(unique(de$Ec))[11])) )
# dev.off()

##plots only Pl,Ll:
# tikz(paste0("plot_","CLC_lses10","_Pl_m2.tex"),width=4, height=3)#plot Pl:
plot( ggplot()
      +ggtitle("")
      +xlab("Year") +ylab("Party Effect") + coord_cartesian(ylim=c(0.5001,0.7)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.7-0.5001)+0.5001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_m2,-2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_m2,-2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_m2,-2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_m2,-2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_m2,-2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_m2,-2,6)) )
# dev.off()
# tikz(paste0("plot_","CLC_lses10","_Ll_m2.tex"),width=4, height=3)#plot Ll:
plot( ggplot()
      +ggtitle("")
      +xlab("Year") +ylab("Leader Effect") + coord_cartesian(ylim=c(0.0001,0.2)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.2-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_m2,-2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_m2,-2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_m2,-2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_m2,-2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_m2,-2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_m2,-2,6)) )
# dev.off()


##starting value for thresholds 2
#time variables:
t0=2#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

CLC_lses10_2=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                       +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                     +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses10_2$coefficients)[names(CLC_lses10_2$coefficients)=="time2:Ll"]="Ll:time2"

#table (hypothesis testing):
stargazer(CLC_lses10_2,no.space = T,single.row = T)
ZtestfunT12(CLC_lses10_2)
ZtestfunT(CLC_lses10_2,2,3)
ZtestfunT(CLC_lses10_2,3,4)
ZtestfunT(CLC_lses10_2,4,5)
ZtestfunT(CLC_lses10_2,5,6)

#plots (median cty):
# tikz(paste0("plot_CLC_lses10_0_PlLl_2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("thresholds offset +2") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_2,2,1,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_2,2,2,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_2,2,3,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_2,2,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_2,2,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_2,2,6,sort(unique(de$Ec))[11])) )
# dev.off()

##plots only Pl,Ll:
# tikz(paste0("plot_","CLC_lses10","_Pl_2.tex"),width=4, height=3)#plot Pl:
plot( ggplot()
      +ggtitle("")
      +xlab("Year") +ylab("Party Effect") + coord_cartesian(ylim=c(0.5001,0.7)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.7-0.5001)+0.5001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_2,2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_2,2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_2,2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_2,2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_2,2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_2,2,6)) )
# dev.off()
# tikz(paste0("plot_","CLC_lses10","_Ll_2.tex"),width=4, height=3)#plot Ll:
plot( ggplot()
      +ggtitle("")
      +xlab("Year") +ylab("Leader Effect") + coord_cartesian(ylim=c(0.0001,0.2)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.2-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_2,2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_2,2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_2,2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_2,2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_2,2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_2,2,6)) )
# dev.off()


##starting value for thresholds 4
#time variables:
t0=4#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

CLC_lses10_4=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                       +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                     +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses10_4$coefficients)[names(CLC_lses10_4$coefficients)=="time2:Ll"]="Ll:time2"

#table (hypothesis testing):
stargazer(CLC_lses10_4,no.space = T,single.row = T)
ZtestfunT12(CLC_lses10_4)
ZtestfunT(CLC_lses10_4,2,3)
ZtestfunT(CLC_lses10_4,3,4)
ZtestfunT(CLC_lses10_4,4,5)
ZtestfunT(CLC_lses10_4,5,6)

#plots (median cty):
# tikz(paste0("plot_CLC_lses10_0_PlLl_4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("thresholds offset +4") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_4,4,1,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_4,4,2,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_4,4,3,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_4,4,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_4,4,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10funCTY(CLC_lses10_4,4,6,sort(unique(de$Ec))[11])) )
# dev.off()

##plots only Pl,Ll:
# tikz(paste0("plot_","CLC_lses10","_Pl_4.tex"),width=4, height=3)#plot Pl:
plot( ggplot()
      +ggtitle("")
      +xlab("Year") +ylab("Party Effect") + coord_cartesian(ylim=c(0.5001,0.7)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.7-0.5001)+0.5001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_4,4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_4,4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_4,4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_4,4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_4,4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses10_4,4,6)) )
# dev.off()
# tikz(paste0("plot_","CLC_lses10","_Ll_4.tex"),width=4, height=3)#plot Ll:
plot( ggplot()
      +ggtitle("")
      +xlab("Year") +ylab("Leader Effect") + coord_cartesian(ylim=c(0.0001,0.2)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.2-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_4,4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_4,4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_4,4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_4,4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_4,4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses10_4,4,6)) )
# dev.off()


##Table I6##
##table full results:
cm4=CLC_lses10_m4#shorten name for stargazer:
cm2=CLC_lses10_m2
c0=CLC_lses10_0
c2=CLC_lses10_2
c4=CLC_lses10_4
stargazer(cm4,cm2,c0,c2,c4)#table
rm(cm4,cm2,c0,c2,c4)


##ANALYSIS 10Y SEGMENTS EU - NON EU:----

#EU dummy:
de$DL[de$Ec %in% c("AUS","CAN","ISR","NZL")]=1#non EU
de$DL[!de$Ec %in% c("AUS","CAN","ISR","NZL")]=0#EU

#confidence interval functions:
CI.PlLllses10CGfun=function(CLC,t0,pe){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=8)#Pl:
  for (t in 1:59) {
    t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    #left:
    CI.PlLl[t,1]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6+
      CLC$coefficients["Pl:time2:DL"]*t2+CLC$coefficients["Pl:time3:DL"]*t3+CLC$coefficients["Pl:time4:DL"]*t4+CLC$coefficients["Pl:time5:DL"]*t5+CLC$coefficients["Pl:time6:DL"]*t6-
      (CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6+
         CLC$coefficients["Ll:time2:DL"]*t2+CLC$coefficients["Ll:time3:DL"]*t3+CLC$coefficients["Ll:time4:DL"]*t4+CLC$coefficients["Ll:time5:DL"]*t5+CLC$coefficients["Ll:time6:DL"]*t6)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      t2^2*vcov(CLC)["Pl:time2:DL","Pl:time2:DL"]+t3^2*vcov(CLC)["Pl:time3:DL","Pl:time3:DL"]+t4^2*vcov(CLC)["Pl:time4:DL","Pl:time4:DL"]+t5^2*vcov(CLC)["Pl:time5:DL","Pl:time5:DL"]+t6^2*vcov(CLC)["Pl:time6:DL","Pl:time6:DL"]+
      1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      t2^2*vcov(CLC)["Ll:time2:DL","Ll:time2:DL"]+t3^2*vcov(CLC)["Ll:time3:DL","Ll:time3:DL"]+t4^2*vcov(CLC)["Ll:time4:DL","Ll:time4:DL"]+t5^2*vcov(CLC)["Ll:time5:DL","Ll:time5:DL"]+t6^2*vcov(CLC)["Ll:time6:DL","Ll:time6:DL"]-
      2*1*1*vcov(CLC)["Pl_ISL","Ll_ISL"]+2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6"]+
      2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2:DL"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2:DL"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3:DL"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3:DL"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4:DL"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4:DL"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5:DL"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5:DL"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6:DL"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6:DL"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2:DL"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2:DL"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3:DL"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3:DL"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4:DL"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4:DL"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5:DL"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5:DL"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6:DL"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6:DL"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]+
      2*t2*t2*vcov(CLC)["Pl:time2","Pl:time2:DL"]-2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2:DL"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3:DL"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3:DL"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4:DL"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4:DL"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5:DL"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5:DL"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6:DL"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6:DL"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t2*t2*vcov(CLC)["Ll:time2","Pl:time2:DL"]+2*t2*t2*vcov(CLC)["Ll:time2","Ll:time2:DL"]-2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3:DL"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3:DL"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4:DL"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4:DL"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5:DL"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5:DL"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6:DL"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]+
      2*t3*t2*vcov(CLC)["Pl:time3","Pl:time2:DL"]-2*t3*t2*vcov(CLC)["Pl:time3","Ll:time2:DL"]+2*t3*t3*vcov(CLC)["Pl:time3","Pl:time3:DL"]-2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t3*t2*vcov(CLC)["Ll:time3","Pl:time2:DL"]+2*t3*t2*vcov(CLC)["Ll:time3","Ll:time2:DL"]-2*t3*t3*vcov(CLC)["Ll:time3","Pl:time3:DL"]+2*t3*t3*vcov(CLC)["Ll:time3","Ll:time3:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]+
      2*t4*t2*vcov(CLC)["Pl:time4","Pl:time2:DL"]-2*t4*t2*vcov(CLC)["Pl:time4","Ll:time2:DL"]+2*t4*t3*vcov(CLC)["Pl:time4","Pl:time3:DL"]-2*t4*t3*vcov(CLC)["Pl:time4","Ll:time3:DL"]+
      2*t4*t4*vcov(CLC)["Pl:time4","Pl:time4:DL"]-2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t4*t2*vcov(CLC)["Ll:time4","Pl:time2:DL"]+2*t4*t2*vcov(CLC)["Ll:time4","Ll:time2:DL"]-2*t4*t3*vcov(CLC)["Ll:time4","Pl:time3:DL"]+2*t4*t3*vcov(CLC)["Ll:time4","Ll:time3:DL"]-
      2*t4*t4*vcov(CLC)["Ll:time4","Pl:time4:DL"]+2*t4*t4*vcov(CLC)["Ll:time4","Ll:time4:DL"]-2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]+
      2*t5*t2*vcov(CLC)["Pl:time5","Pl:time2:DL"]-2*t5*t2*vcov(CLC)["Pl:time5","Ll:time2:DL"]+2*t5*t3*vcov(CLC)["Pl:time5","Pl:time3:DL"]-2*t5*t3*vcov(CLC)["Pl:time5","Ll:time3:DL"]+
      2*t5*t4*vcov(CLC)["Pl:time5","Pl:time4:DL"]-2*t5*t4*vcov(CLC)["Pl:time5","Ll:time4:DL"]+2*t5*t5*vcov(CLC)["Pl:time5","Pl:time5:DL"]-2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t5*t2*vcov(CLC)["Ll:time5","Pl:time2:DL"]+2*t5*t2*vcov(CLC)["Ll:time5","Ll:time2:DL"]-2*t5*t3*vcov(CLC)["Ll:time5","Pl:time3:DL"]+2*t5*t3*vcov(CLC)["Ll:time5","Ll:time3:DL"]-
      2*t5*t4*vcov(CLC)["Ll:time5","Pl:time4:DL"]+2*t5*t4*vcov(CLC)["Ll:time5","Ll:time4:DL"]-2*t5*t5*vcov(CLC)["Ll:time5","Pl:time5:DL"]+2*t5*t5*vcov(CLC)["Ll:time5","Ll:time5:DL"]-2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]+
      2*t6*t2*vcov(CLC)["Pl:time6","Pl:time2:DL"]-2*t6*t2*vcov(CLC)["Pl:time6","Ll:time2:DL"]+2*t6*t3*vcov(CLC)["Pl:time6","Pl:time3:DL"]-2*t6*t3*vcov(CLC)["Pl:time6","Ll:time3:DL"]+
      2*t6*t4*vcov(CLC)["Pl:time6","Pl:time4:DL"]-2*t6*t4*vcov(CLC)["Pl:time6","Ll:time4:DL"]+2*t6*t5*vcov(CLC)["Pl:time6","Pl:time5:DL"]-2*t6*t5*vcov(CLC)["Pl:time6","Ll:time5:DL"]+2*t6*t6*vcov(CLC)["Pl:time6","Pl:time6:DL"]-2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6:DL"]-
      2*t6*t2*vcov(CLC)["Ll:time6","Pl:time2:DL"]+2*t6*t2*vcov(CLC)["Ll:time6","Ll:time2:DL"]-2*t6*t3*vcov(CLC)["Ll:time6","Pl:time3:DL"]+2*t6*t3*vcov(CLC)["Ll:time6","Ll:time3:DL"]-
      2*t6*t4*vcov(CLC)["Ll:time6","Pl:time4:DL"]+2*t6*t4*vcov(CLC)["Ll:time6","Ll:time4:DL"]-2*t6*t5*vcov(CLC)["Ll:time6","Pl:time5:DL"]+2*t6*t5*vcov(CLC)["Ll:time6","Ll:time5:DL"]-2*t6*t6*vcov(CLC)["Ll:time6","Pl:time6:DL"]+2*t6*t6*vcov(CLC)["Ll:time6","Ll:time6:DL"]-
      2*t2*t2*vcov(CLC)["Pl:time2:DL","Ll:time2:DL"]+2*t2*t3*vcov(CLC)["Pl:time2:DL","Pl:time3:DL"]-2*t2*t3*vcov(CLC)["Pl:time2:DL","Ll:time3:DL"]+
      2*t2*t4*vcov(CLC)["Pl:time2:DL","Pl:time4:DL"]-2*t2*t4*vcov(CLC)["Pl:time2:DL","Ll:time4:DL"]+2*t2*t5*vcov(CLC)["Pl:time2:DL","Pl:time5:DL"]-2*t2*t5*vcov(CLC)["Pl:time2:DL","Ll:time5:DL"]+2*t2*t6*vcov(CLC)["Pl:time2:DL","Pl:time6:DL"]-2*t2*t6*vcov(CLC)["Pl:time2:DL","Ll:time6:DL"]-
      2*t2*t3*vcov(CLC)["Ll:time2:DL","Pl:time3:DL"]+2*t2*t3*vcov(CLC)["Ll:time2:DL","Ll:time3:DL"]-
      2*t2*t4*vcov(CLC)["Ll:time2:DL","Pl:time4:DL"]+2*t2*t4*vcov(CLC)["Ll:time2:DL","Ll:time4:DL"]-2*t2*t5*vcov(CLC)["Ll:time2:DL","Pl:time5:DL"]+2*t2*t5*vcov(CLC)["Ll:time2:DL","Ll:time5:DL"]-2*t2*t6*vcov(CLC)["Ll:time2:DL","Pl:time6:DL"]+2*t2*t6*vcov(CLC)["Ll:time2:DL","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3:DL","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3:DL","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3:DL","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3:DL","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3:DL","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3:DL","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3:DL","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3:DL","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3:DL","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3:DL","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3:DL","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3:DL","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3:DL","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4:DL","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4:DL","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4:DL","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4:DL","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4:DL","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4:DL","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4:DL","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4:DL","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4:DL","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5:DL","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5:DL","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5:DL","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5:DL","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5:DL","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6:DL","Ll:time6:DL"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
    #right:
    CI.PlLl[t,5]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6-
      (CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6)#fitted values
    CI.PlLl[t,6]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]-
      2*1*1*vcov(CLC)["Pl_ISL","Ll_ISL"]+2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]
    CI.PlLl[t,7]=CI.PlLl[t,5]-qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI lower
    CI.PlLl[t,8]=CI.PlLl[t,5]+qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}#wo Pl:time1:DL,Ll:time1:DL

#Z-test functions:
ZtestfunT12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTD12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2,":DL")]-CLC$coefficients[paste0("Ll:time",2,":DL")]#mean
  v=vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Pl:time",2,":DL")]+vcov(CLC)[paste0("Ll:time",2,":DL"),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Ll:time",2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunD12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]+
    CLC$coefficients[paste0("Pl:time",2,":DL")]-CLC$coefficients[paste0("Ll:time",2,":DL")]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]+
    vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Pl:time",2,":DL")]+vcov(CLC)[paste0("Ll:time",2,":DL"),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]+2*vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",2),paste0("Pl:time",2,":DL")]+2*vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Ll:time",2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunT=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTD=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1,":DL")]-CLC$coefficients[paste0("Ll:time",per1,":DL")]-
        CLC$coefficients[paste0("Pl:time",per2,":DL")]+CLC$coefficients[paste0("Ll:time",per2,":DL")])#mean
  v=vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per1,":DL")]+vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per1,":DL")]+
    vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Pl:time",per2,":DL")]+vcov(CLC)[paste0("Ll:time",per2,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2,":DL")]-2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Ll:time",per2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunD=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]+
        CLC$coefficients[paste0("Pl:time",per1,":DL")]-CLC$coefficients[paste0("Ll:time",per1,":DL")]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)]-
        CLC$coefficients[paste0("Pl:time",per2,":DL")]+CLC$coefficients[paste0("Ll:time",per2,":DL")])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per1,":DL")]+vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per1,":DL")]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]+
    vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Pl:time",per2,":DL")]+vcov(CLC)[paste0("Ll:time",per2,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]+2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per1,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1,":DL")]+2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2)]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2)]-
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per2),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Ll:time",per2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regression:
sort(unique(de$Ey[de$DL==1]))#full coverage DL, hence full interactions
sort(unique(de$Ey[de$DL==0]))#full coverage non DL, hence full interactions

sort(unique(de$Es[de$DL==1]))


CLC_lses10_EU=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                        Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                        +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                      +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses10_EU$coefficients)[names(CLC_lses10_EU$coefficients)=="time2:Ll"]="Ll:time2"
names(CLC_lses10_EU$coefficients)[names(CLC_lses10_EU$coefficients)=="time2:Ll:DL"]="Ll:time2:DL"


##Table H1## and ##Table I16##
#hypothesis testing:
stargazer(CLC_lses10_EU)
#drop EU (DL==0):
ZtestfunT12(CLC_lses10_EU)
ZtestfunT(CLC_lses10_EU,2,3)
ZtestfunT(CLC_lses10_EU,3,4)
ZtestfunT(CLC_lses10_EU,4,5)
ZtestfunT(CLC_lses10_EU,5,6)
#drop non EU (DL==1):
ZtestfunD12(CLC_lses10_EU)
ZtestfunD(CLC_lses10_EU,2,3)
ZtestfunD(CLC_lses10_EU,3,4)
ZtestfunD(CLC_lses10_EU,4,5)
ZtestfunD(CLC_lses10_EU,5,6)
#difference in drop between non EU and EU (DL==1 vs DL==0):
ZtestfunTD12(CLC_lses10_EU)
ZtestfunTD(CLC_lses10_EU,2,3)
ZtestfunTD(CLC_lses10_EU,3,4)
ZtestfunTD(CLC_lses10_EU,4,5)
ZtestfunTD(CLC_lses10_EU,5,6)
#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_EU.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.84)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.84-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_EU,0,1)) +#(high)
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_EU,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_EU,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_EU,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_EU,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_EU,0,6)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_EU,0,1)) +#(low)
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_EU,0,2)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_EU,0,3)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_EU,0,4)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_EU,0,5)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_EU,0,6)) )
# dev.off()


#ANALYSIS MARGINAL EFFECTS:----

#DEU_1998(#6) versus DEU_2013(#6):
#(what would have happened in 2013 with the weights of 1998)

#dummies for election-choice FE:
sort(unique(de$Esalt[de$Es=="DEU_1998"]))
#"DEU_19981" "DEU_19982" "DEU_19983" "DEU_19984" "DEU_19985" "DEU_19986"
sort(unique(de$Esalt[de$Es=="DEU_2013"]))
#"DEU_20131" "DEU_20132" "DEU_20133" "DEU_20134" "DEU_20135" "DEU_20136"
de$D981=ifelse(I(de$Esalt=="DEU_19981")==T,1,0)
de$D982=ifelse(I(de$Esalt=="DEU_19982")==T,1,0)
de$D983=ifelse(I(de$Esalt=="DEU_19983")==T,1,0)
de$D984=ifelse(I(de$Esalt=="DEU_19984")==T,1,0)
de$D985=ifelse(I(de$Esalt=="DEU_19985")==T,1,0)
de$D986=ifelse(I(de$Esalt=="DEU_19986")==T,1,0)
de$D131=ifelse(I(de$Esalt=="DEU_20131")==T,1,0)
de$D132=ifelse(I(de$Esalt=="DEU_20132")==T,1,0)
de$D133=ifelse(I(de$Esalt=="DEU_20133")==T,1,0)
de$D134=ifelse(I(de$Esalt=="DEU_20134")==T,1,0)
de$D135=ifelse(I(de$Esalt=="DEU_20135")==T,1,0)
de$D136=ifelse(I(de$Esalt=="DEU_20136")==T,1,0)

#regressions:
CLC_me_98=clogit(Va ~ Pl+Ll+D981+D982+D983+D984+D985+D986+strata(chid)
                 , robust=T, data=de[de$Es=="DEU_1998",], method="efron")
CLC_me_13=clogit(Va ~ Pl+Ll+D131+D132+D133+D134+D135+D136+strata(chid)
                 , robust=T, data=de[de$Es=="DEU_2013",], method="efron")

#predicted vote:
de$FEec=ifelse(de$alt==1, CLC_me_13$coefficients[3],#code fixed effects variable
               ifelse(de$alt==2, CLC_me_13$coefficients[4],
                      ifelse(de$alt==3, CLC_me_13$coefficients[5],
                             ifelse(de$alt==4, CLC_me_13$coefficients[6],0))))
de$Prnum=exp(CLC_me_13$coefficients[1]*de$Pl+CLC_me_13$coefficients[2]*de$Ll+de$FEec)#numerator for Pr
de=de %>% group_by(chid) %>% mutate(Prden=sum(Prnum,na.rm=T))#denominator for Pr
de$Pr=de$Prnum/de$Prden#individual predicted Pr
sum(de$Pr[de$Es=="DEU_2013"&de$alt==1],na.rm=T)#cumulative Pr == predicted vote
sum(de$Pr[de$Es=="DEU_2013"&de$alt==2],na.rm=T)
sum(de$Pr[de$Es=="DEU_2013"&de$alt==3],na.rm=T)
sum(de$Pr[de$Es=="DEU_2013"&de$alt==4],na.rm=T)
sum(de$Pr[de$Es=="DEU_2013"&de$alt==5],na.rm=T)
sum(de$Pr[de$Es=="DEU_2013"&de$alt==6],na.rm=T)

table(de$alt[de$Es=="DEU_2013"&de$Va==T])#reported vote
#ok, predicted vote == reported vote, Pr calculus is correct!

#counterfactual vote:
de$PrnumCF=exp(CLC_me_98$coefficients[1]*de$Pl+CLC_me_98$coefficients[2]*de$Ll+de$FEec)#numerator for Pr
de=de %>% group_by(chid) %>% mutate(PrdenCF=sum(PrnumCF,na.rm=T))#denominator for Pr
de$PrCF=de$PrnumCF/de$PrdenCF#individual predicted Pr
sum(de$PrCF[de$Es=="DEU_2013"&de$alt==1],na.rm=T)#cumulative Pr == predicted vote
sum(de$PrCF[de$Es=="DEU_2013"&de$alt==2],na.rm=T)
sum(de$PrCF[de$Es=="DEU_2013"&de$alt==3],na.rm=T)
sum(de$PrCF[de$Es=="DEU_2013"&de$alt==4],na.rm=T)
sum(de$PrCF[de$Es=="DEU_2013"&de$alt==5],na.rm=T)
sum(de$PrCF[de$Es=="DEU_2013"&de$alt==6],na.rm=T)

#change (ratio) in vote:
sum(de$PrCF[de$Es=="DEU_2013"&de$alt==1],na.rm=T)/sum(de$Pr[de$Es=="DEU_2013"&de$alt==1],na.rm=T)#0.907
sum(de$PrCF[de$Es=="DEU_2013"&de$alt==2],na.rm=T)/sum(de$Pr[de$Es=="DEU_2013"&de$alt==2],na.rm=T)#1.004
sum(de$PrCF[de$Es=="DEU_2013"&de$alt==3],na.rm=T)/sum(de$Pr[de$Es=="DEU_2013"&de$alt==3],na.rm=T)#1.067
sum(de$PrCF[de$Es=="DEU_2013"&de$alt==4],na.rm=T)/sum(de$Pr[de$Es=="DEU_2013"&de$alt==4],na.rm=T)#1.107
sum(de$PrCF[de$Es=="DEU_2013"&de$alt==5],na.rm=T)/sum(de$Pr[de$Es=="DEU_2013"&de$alt==5],na.rm=T)#1.633

#counterfactual vote share:
#CDU/CSU (34.1+7.4) 41.5 original vote share * 0.907 change in vote share = 37.640
#SPD 25.7 * 1.004 = 25.802
#DIE LINKE 8.6 * 1.067 = 9.176
#GRUENE 8.4 * 1.107 = 9.298
#FDP 4.8 * 1.633 = 7.838

#counterfactual seat share:
#tot vote share parties >5%: 37.640+25.802+9.176+9.298+7.838=89.754
#CDU/CSU 37.640/89.754 = 0.419
#SPD 25.802/89.754 = 0.287
#DIE LINKE 9.176/89.754 = 0.102
#GRUENE 9.298/89.754 = 0.103
#FDP 7.838/89.754 = 0.087
#govt CDU/CSU + FDP = 0.419 + 0.087 = 0.506
# (in the parliament = 0.506*631seats = 319 vs 316 majority)
# (NB not considering possible difference in overhang and leveling seats due to the FDP gaining seats)


#ALTERNATIVE SPECIFICATIONS SPLINE TWO PERIODS:----

#confidence interval functions:
CI.PlLlsesfunCTY=function(CLC,tr,cty){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    t1=pmin(t,tr)
    t2=pmax(tr,t)
    CI.PlLl[t,1]=CLC$coefficients[paste0("Pl_",cty)]+CLC$coefficients["Pl:time1"]*t1+CLC$coefficients["Pl:time2"]*t2-
      (CLC$coefficients[paste0("Ll_",cty)]+CLC$coefficients["Ll:time1"]*t1+CLC$coefficients["Ll:time2"]*t2)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)[paste0("Pl_",cty),paste0("Pl_",cty)]+t1^2*vcov(CLC)["Pl:time1","Pl:time1"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+1*vcov(CLC)[paste0("Ll_",cty),paste0("Ll_",cty)]+t1^2*vcov(CLC)["Ll:time1","Ll:time1"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+
      2*t1*vcov(CLC)[paste0("Pl_",cty),"Pl:time1"]+2*t2*vcov(CLC)[paste0("Pl_",cty),"Pl:time2"]-2*vcov(CLC)[paste0("Pl_",cty),paste0("Ll_",cty)]-2*t1*vcov(CLC)[paste0("Pl_",cty),"Ll:time1"]-2*t2*vcov(CLC)[paste0("Pl_",cty),"Ll:time2"]+
      2*t1*t2*vcov(CLC)["Pl:time1","Pl:time2"]-2*t1*vcov(CLC)[paste0("Ll_",cty),"Pl:time1"]-2*t1^2*vcov(CLC)["Pl:time1","Ll:time1"]-2*t1*t2*vcov(CLC)["Pl:time1","Ll:time2"]-
      2*t2*vcov(CLC)[paste0("Ll_",cty),"Pl:time2"]-2*t1*t2*vcov(CLC)["Ll:time1","Pl:time2"]-2*t2^2*vcov(CLC)["Pl:time2","Ll:time2"]+
      2*t1*vcov(CLC)[paste0("Ll_",cty),"Ll:time1"]+2*t2*vcov(CLC)[paste0("Ll_",cty),"Ll:time2"]+
      2*t1*t2*vcov(CLC)["Ll:time1","Ll:time2"]#Variance new
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  CI.PlLl
}#by cty

#regressions:
tr=38
de$time1=pmin(de$time,tr)#spline functions for coefficients the slopes in each segment
de$time2=pmax(tr,de$time)
CLC_ses=clogit(Va ~ Pl:time1+Ll:time1+Pl:time2+Ll:time2
                  +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                  +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_ses$coefficients)[names(CLC_ses$coefficients)=="time1:Ll"]="Ll:time1"
# , weights=Rwc
trCLC_ses=tr#threshold

#list and test countries by average variance:
vaCLC_ses=matrix(NA,nrow=length(sort(unique(de$Ec))),ncol=2)#average variance by country over time
for(i in 1:length(sort(unique(de$Ec)))) {#get average variance over time
  vaCLC_ses[i,1]=paste0(sort(unique(de$Ec))[i])
  vaCLC_ses[i,2]=mean(CI.PlLlsesfunCTY(CLC_ses,trCLC_ses,sort(unique(de$Ec))[i])[,2],na.rm=T)
}
vaCLC_ses=as.data.frame(vaCLC_ses)
vaCLC_ses=vaCLC_ses[order(vaCLC_ses$V2),]
vaCLC_ses[9,1]#country with median variance (ISL#wow)
vaCLC_ses[,1]#list of countries by variance (small to high)
sort(table(ds$Ec),decreasing=T)#list of countries by elections coded (big to low)
#the 9 countries with higher variance are also the 9 with less elections coded!!!


##Table C1## Two-Period Spline
#plots (median cty):
# tikz(paste0("plot_","CLC_ses","_PlLl_ctymed.tex"),width=4, height=3)#plot Pl-Ll:
plot(ggplot(CI.PlLlsesfunCTY(CLC_ses,trCLC_ses,vaCLC_ses[9,1]), aes(x=t,y=V1)) 
     +ggtitle("Two-Period Spline") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) +
       theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
             panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
       geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
       geom_smooth(aes(ymin=V3,ymax=V4),stat="identity", lwd=2) )
# dev.off()


#ALTERNATIVE SPECIFICATIONS SPLINE THREE PERIODS:----

#confidence interval functions:
CI.PlLlsessfunCTY=function(CLC,tr1,tr2,cty){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    t1=pmin(t,tr1)
    t2=pmax(tr1,pmin(t,tr2))
    t3=pmax(tr2,t)
    CI.PlLl[t,1]=CLC$coefficients[paste0("Pl_",cty)]+CLC$coefficients["Pl:time1"]*t1+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3-
      (CLC$coefficients[paste0("Ll_",cty)]+CLC$coefficients["Ll:time1"]*t1+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)[paste0("Pl_",cty),paste0("Pl_",cty)]+t1^2*vcov(CLC)["Pl:time1","Pl:time1"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+1*vcov(CLC)[paste0("Ll_",cty),paste0("Ll_",cty)]+t1^2*vcov(CLC)["Ll:time1","Ll:time1"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+
      2*t1*vcov(CLC)[paste0("Pl_",cty),"Pl:time1"]+2*t2*vcov(CLC)[paste0("Pl_",cty),"Pl:time2"]+2*t3*vcov(CLC)[paste0("Pl_",cty),"Pl:time3"]-2*vcov(CLC)[paste0("Pl_",cty),paste0("Ll_",cty)]-2*t1*vcov(CLC)[paste0("Pl_",cty),"Ll:time1"]-2*t2*vcov(CLC)[paste0("Pl_",cty),"Ll:time2"]-2*t3*vcov(CLC)[paste0("Pl_",cty),"Ll:time3"]+
      2*t1*t2*vcov(CLC)["Pl:time1","Pl:time2"]+2*t1*t3*vcov(CLC)["Pl:time1","Pl:time3"]-2*t1*vcov(CLC)["Pl:time1",paste0("Ll_",cty)]-2*t1^2*vcov(CLC)["Pl:time1","Ll:time1"]-2*t1*t2*vcov(CLC)["Pl:time1","Ll:time2"]-2*t1*t3*vcov(CLC)["Pl:time1","Ll:time3"]+
      2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*vcov(CLC)["Pl:time2",paste0("Ll_",cty)]-2*t1*t2*vcov(CLC)["Pl:time2","Ll:time1"]-2*t2^2*vcov(CLC)["Pl:time2","Ll:time2"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]-
      2*t3*vcov(CLC)["Pl:time3",paste0("Ll_",cty)]-2*t3*t1*vcov(CLC)["Pl:time3","Ll:time1"]-2*t3*t2*vcov(CLC)["Pl:time3","Ll:time2"]-2*t3^2*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t1*vcov(CLC)[paste0("Ll_",cty),"Ll:time1"]+2*t2*vcov(CLC)[paste0("Ll_",cty),"Ll:time2"]+2*t3*vcov(CLC)[paste0("Ll_",cty),"Ll:time3"]+
      2*t1*t2*vcov(CLC)["Ll:time1","Ll:time2"]+2*t1*t3*vcov(CLC)["Ll:time1","Ll:time3"]+
      2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  CI.PlLl
}

#regressions:
tr1=9
tr2=37
de$time1=pmin(de$time,tr1)#spline functions for coefficients the slopes in each segment
de$time2=pmax(tr1,pmin(de$time,tr2))
de$time3=pmax(tr2,de$time)
CLC_sess=clogit(Va ~ Pl:time1+Ll:time1+Pl:time2+Ll:time2+Pl:time3+Ll:time3
                   +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                   +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_sess$coefficients)[names(CLC_sess$coefficients)=="time1:Ll"]="Ll:time1"
tr1CLC_sess=tr1#
tr2CLC_sess=tr2#

#list and test countries by average variance:
vaCLC_sess=matrix(NA,nrow=length(sort(unique(de$Ec))),ncol=2)#average variance by country over time
for(i in 1:length(sort(unique(de$Ec)))) {#get average variance over time
  vaCLC_sess[i,1]=paste0(sort(unique(de$Ec))[i])
  vaCLC_sess[i,2]=mean(CI.PlLlsessfunCTY(CLC_sess,tr1CLC_sess,tr2CLC_sess,sort(unique(de$Ec))[i])[,2],na.rm=T)
}
vaCLC_sess=as.data.frame(vaCLC_sess)
vaCLC_sess=vaCLC_sess[order(vaCLC_sess$V2),]
vaCLC_sess[9,1]#country with median variance
vaCLC_sess[,1]#list of countries by variance (small to high)#virtually the same than two periods


##Table C1## Three-Period Spline
#plots (median cty):
# tikz(paste0("plot_","CLC_sess","_PlLl_ctymed.tex"),width=4, height=3)#plot Pl-Ll:
plot(ggplot(CI.PlLlsessfunCTY(CLC_sess,tr1CLC_sess,tr2CLC_sess,vaCLC_sess[9,1]), aes(x=t,y=V1)) 
     +ggtitle("Three-Period Spline") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) +
       theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
             panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
       geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
       geom_smooth(aes(ymin=V3,ymax=V4),stat="identity", lwd=2) )
# dev.off()


#ALTERNATIVE SPECIFICATIONS SPLINE FOUR PERIODS:----

#confidence interval functions:
CI.PlLlsesssfunCTY=function(CLC,tr1,tr2,tr3,cty){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    t1=pmin(t,tr1)
    t2=pmax(tr1,pmin(t,tr2))
    t3=pmax(tr2,pmin(t,tr3))
    t4=pmax(tr3,t)
    CI.PlLl[t,1]=CLC$coefficients[paste0("Pl_",cty)]+CLC$coefficients["Pl:time1"]*t1+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4-
      (CLC$coefficients[paste0("Ll_",cty)]+CLC$coefficients["Ll:time1"]*t1+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)[paste0("Pl_",cty),paste0("Pl_",cty)]+t1^2*vcov(CLC)["Pl:time1","Pl:time1"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+1*vcov(CLC)[paste0("Ll_",cty),paste0("Ll_",cty)]+t1^2*vcov(CLC)["Ll:time1","Ll:time1"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+
      2*t1*vcov(CLC)[paste0("Pl_",cty),"Pl:time1"]+2*t2*vcov(CLC)[paste0("Pl_",cty),"Pl:time2"]+2*t3*vcov(CLC)[paste0("Pl_",cty),"Pl:time3"]+2*t4*vcov(CLC)[paste0("Pl_",cty),"Pl:time4"]-2*vcov(CLC)[paste0("Pl_",cty),paste0("Ll_",cty)]-2*t1*vcov(CLC)[paste0("Pl_",cty),"Ll:time1"]-2*t2*vcov(CLC)[paste0("Pl_",cty),"Ll:time2"]-2*t3*vcov(CLC)[paste0("Pl_",cty),"Ll:time3"]-2*t4*vcov(CLC)[paste0("Pl_",cty),"Ll:time4"]+
      2*t1*t2*vcov(CLC)["Pl:time1","Pl:time2"]+2*t1*t3*vcov(CLC)["Pl:time1","Pl:time3"]+2*t1*t4*vcov(CLC)["Pl:time1","Pl:time4"]-2*t1*vcov(CLC)["Pl:time1",paste0("Ll_",cty)]-2*t1^2*vcov(CLC)["Pl:time1","Ll:time1"]-2*t1*t2*vcov(CLC)["Pl:time1","Ll:time2"]-2*t1*t3*vcov(CLC)["Pl:time1","Ll:time3"]-2*t1*t4*vcov(CLC)["Pl:time1","Ll:time4"]+
      2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]+2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*vcov(CLC)["Pl:time2",paste0("Ll_",cty)]-2*t1*t2*vcov(CLC)["Pl:time2","Ll:time1"]-2*t2^2*vcov(CLC)["Pl:time2","Ll:time2"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*vcov(CLC)["Pl:time3",paste0("Ll_",cty)]-2*t3*t1*vcov(CLC)["Pl:time3","Ll:time1"]-2*t3*t2*vcov(CLC)["Pl:time3","Ll:time2"]-2*t3^2*vcov(CLC)["Pl:time3","Ll:time3"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]-
      2*t4*vcov(CLC)["Pl:time4",paste0("Ll_",cty)]-2*t4*t1*vcov(CLC)["Pl:time4","Ll:time1"]-2*t4*t2*vcov(CLC)["Pl:time4","Ll:time2"]-2*t4*t3*vcov(CLC)["Pl:time4","Ll:time3"]-2*t4^2*vcov(CLC)["Pl:time4","Ll:time4"]+
      2*t1*vcov(CLC)[paste0("Ll_",cty),"Ll:time1"]+2*t2*vcov(CLC)[paste0("Ll_",cty),"Ll:time2"]+2*t3*vcov(CLC)[paste0("Ll_",cty),"Ll:time3"]+2*t4*vcov(CLC)[paste0("Ll_",cty),"Ll:time4"]+
      2*t1*t2*vcov(CLC)["Ll:time1","Ll:time2"]+2*t1*t3*vcov(CLC)["Ll:time1","Ll:time3"]+2*t1*t4*vcov(CLC)["Ll:time1","Ll:time4"]+
      2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]+
      2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]#Variance
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  CI.PlLl
}

#regressions:
tr1=13
tr2=14
tr3=37
de$time1=pmin(de$time,tr1)#spline functions for coefficients the slopes in each segment
de$time2=pmax(tr1,pmin(de$time,tr2))
de$time3=pmax(tr2,pmin(de$time,tr3))
de$time4=pmax(tr3,de$time)
CLC_sesss=clogit(Va ~ Pl:time1+Ll:time1+Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4
                    +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                    +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_sesss$coefficients)[names(CLC_sesss$coefficients)=="time1:Ll"]="Ll:time1"
tr1CLC_sesss=tr1#
tr2CLC_sesss=tr2#
tr3CLC_sesss=tr3#

#list and test countries by average variance:
vaCLC_sesss=matrix(NA,nrow=length(sort(unique(de$Ec))),ncol=2)#average variance by country over time
for(i in 1:length(sort(unique(de$Ec)))) {#get average variance over time
  vaCLC_sesss[i,1]=paste0(sort(unique(de$Ec))[i])
  vaCLC_sesss[i,2]=mean(CI.PlLlsesssfunCTY(CLC_sesss,tr1CLC_sesss,tr2CLC_sesss,tr3CLC_sesss,sort(unique(de$Ec))[i])[,2],na.rm=T)
}
vaCLC_sesss=as.data.frame(vaCLC_sesss)
vaCLC_sesss=vaCLC_sesss[order(vaCLC_sesss$V2),]
vaCLC_sesss[9,1]#country with median variance
vaCLC_sesss[,1]#list of countries by variance (small to high)#virtually the same than two periods


##Table C1## Four-Period Spline
#plots:
#median cty:
# tikz(paste0("plot_","CLC_sesss","_PlLl_ctymed.tex"),width=4, height=3)#plot Pl-Ll:
plot(ggplot(CI.PlLlsesssfunCTY(CLC_sesss,tr1CLC_sesss,tr2CLC_sesss,tr3CLC_sesss,vaCLC_sesss[9,1]), aes(x=t,y=V1)) 
     +ggtitle("Four-Period Spline") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) +
       theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
             panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
       geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
       geom_smooth(aes(ymin=V3,ymax=V4),stat="identity", lwd=2) )
# dev.off()


#ALTERNATIVE SPECIFICATIONS SPLINE FIVE PERIODS:----

#confidence interval functions:
CI.PlLlsessssfunCTY=function(CLC,tr1,tr2,tr3,tr4,cty){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    t1=pmin(t,tr1)
    t2=pmax(tr1,pmin(t,tr2))
    t3=pmax(tr2,pmin(t,tr3))
    t4=pmax(tr3,pmin(t,tr4))
    t5=pmax(tr4,t)
    CI.PlLl[t,1]=CLC$coefficients[paste0("Pl_",cty)]+CLC$coefficients["Pl:time1"]*t1+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5-
      (CLC$coefficients[paste0("Ll_",cty)]+CLC$coefficients["Ll:time1"]*t1+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)[paste0("Pl_",cty),paste0("Pl_",cty)]+t1^2*vcov(CLC)["Pl:time1","Pl:time1"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+
      1*vcov(CLC)[paste0("Ll_",cty),paste0("Ll_",cty)]+t1^2*vcov(CLC)["Ll:time1","Ll:time1"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]-
      2*1*1*vcov(CLC)[paste0("Pl_",cty),paste0("Ll_",cty)]+2*1*t1*vcov(CLC)[paste0("Pl_",cty),"Pl:time1"]-2*1*t1*vcov(CLC)[paste0("Pl_",cty),"Ll:time1"]+2*1*t2*vcov(CLC)[paste0("Pl_",cty),"Pl:time2"]-2*1*t2*vcov(CLC)[paste0("Pl_",cty),"Ll:time2"]+2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Pl:time3"]-2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Ll:time3"]+
      2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Pl:time4"]-2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Ll:time4"]+2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Pl:time5"]-2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Ll:time5"]-
      2*1*t1*vcov(CLC)[paste0("Ll_",cty),"Pl:time1"]+2*1*t1*vcov(CLC)[paste0("Ll_",cty),"Ll:time1"]-2*1*t2*vcov(CLC)[paste0("Ll_",cty),"Pl:time2"]+2*1*t2*vcov(CLC)[paste0("Ll_",cty),"Ll:time2"]-2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Pl:time3"]+2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Ll:time3"]-
      2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Pl:time4"]+2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Ll:time4"]-2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Pl:time5"]+2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Ll:time5"]-
      2*t1*t1*vcov(CLC)["Pl:time1","Ll:time1"]+2*t1*t2*vcov(CLC)["Pl:time1","Pl:time2"]-2*t1*t2*vcov(CLC)["Pl:time1","Ll:time2"]+2*t1*t3*vcov(CLC)["Pl:time1","Pl:time3"]-2*t1*t3*vcov(CLC)["Pl:time1","Ll:time3"]+
      2*t1*t4*vcov(CLC)["Pl:time1","Pl:time4"]-2*t1*t4*vcov(CLC)["Pl:time1","Ll:time4"]+2*t1*t5*vcov(CLC)["Pl:time1","Pl:time5"]-2*t1*t5*vcov(CLC)["Pl:time1","Ll:time5"]-
      2*t1*t2*vcov(CLC)["Ll:time1","Pl:time2"]+2*t1*t2*vcov(CLC)["Ll:time1","Ll:time2"]-2*t1*t3*vcov(CLC)["Ll:time1","Pl:time3"]+2*t1*t3*vcov(CLC)["Ll:time1","Ll:time3"]-
      2*t1*t4*vcov(CLC)["Ll:time1","Pl:time4"]+2*t1*t4*vcov(CLC)["Ll:time1","Ll:time4"]-2*t1*t5*vcov(CLC)["Ll:time1","Pl:time5"]+2*t1*t5*vcov(CLC)["Ll:time1","Ll:time5"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  CI.PlLl
}

#regressions:
tr1=8
tr2=13
tr3=14
tr4=37
de$time1=pmin(de$time,tr1)#spline functions for coefficients the slopes in each segment
de$time2=pmax(tr1,pmin(de$time,tr2))
de$time3=pmax(tr2,pmin(de$time,tr3))
de$time4=pmax(tr3,pmin(de$time,tr4))
de$time5=pmax(tr4,de$time)
CLC_sessss=clogit(Va ~ Pl:time1+Ll:time1+Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5
                      +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                      +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_sessss$coefficients)[names(CLC_sessss$coefficients)=="time1:Ll"]="Ll:time1"
tr1CLC_sessss=tr1#
tr2CLC_sessss=tr2#
tr3CLC_sessss=tr3#
tr4CLC_sessss=tr4#

#list and test countries by average variance:
vaCLC_sessss=matrix(NA,nrow=length(sort(unique(de$Ec))),ncol=2)#average variance by country over time
for(i in 1:length(sort(unique(de$Ec)))) {#get average variance over time
  vaCLC_sessss[i,1]=paste0(sort(unique(de$Ec))[i])
  vaCLC_sessss[i,2]=mean(CI.PlLlsessssfunCTY(CLC_sessss,tr1CLC_sessss,tr2CLC_sessss,tr3CLC_sessss,tr4CLC_sessss,sort(unique(de$Ec))[i])[,2],na.rm=T)
}
vaCLC_sessss=as.data.frame(vaCLC_sessss)
vaCLC_sessss=vaCLC_sessss[order(vaCLC_sessss$V2),]
vaCLC_sessss[9,1]#country with median variance
vaCLC_sessss[,1]#list of countries by variance (small to high)#virtually the same than two periods


##Table C1## Five-Period Spline
#plots:
#median cty:
# tikz(paste0("plot_","CLC_sessss","_PlLl_ctymed.tex"),width=4, height=3)#plot Pl-Ll:
plot(ggplot(CI.PlLlsessssfunCTY(CLC_sessss,tr1CLC_sessss,tr2CLC_sessss,tr3CLC_sessss,tr4CLC_sessss,vaCLC_sessss[9,1]), aes(x=t,y=V1)) 
     +ggtitle("Five-Period Spline") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,1.15)) + scale_y_continuous(expand=c(0,0)) +
       theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
             panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
       geom_histogram(aes(x=Ey,y=((Eyn*(1.15-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
       geom_smooth(aes(ymin=V3,ymax=V4),stat="identity", lwd=2) )
# dev.off()


#ALTERNATIVE SPECIFICATIONS SPLINE COEFFICIENTS:----

#Z-test functions:
ZtestfunS=function(CLC,per){
  m=CLC$coefficients[paste0("Pl:time",per)]-CLC$coefficients[paste0("Ll:time",per)]#mean
  v=vcov(CLC)[paste0("Pl:time",per),paste0("Pl:time",per)]+vcov(CLC)[paste0("Ll:time",per),paste0("Ll:time",per)]-
    2*vcov(CLC)[paste0("Pl:time",per),paste0("Ll:time",per)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}


##Table C1## coefficients
#need to put in the table Party - Leader Likability instead than separate coefficients:
ZtestfunS(CLC_ses,1)
ZtestfunS(CLC_ses,2)
ZtestfunS(CLC_sess,1)
ZtestfunS(CLC_sess,2)
ZtestfunS(CLC_sess,3)
ZtestfunS(CLC_sesss,1)
ZtestfunS(CLC_sesss,2)
ZtestfunS(CLC_sesss,3)
ZtestfunS(CLC_sesss,4)
ZtestfunS(CLC_sessss,1)
ZtestfunS(CLC_sessss,2)
ZtestfunS(CLC_sessss,3)
ZtestfunS(CLC_sessss,4)
ZtestfunS(CLC_sessss,5)


#ALTERNATIVE SPECIFICATIONS SEGMENT SPLINE AND FULL TABLE:----

#confidence interval functions:
CI.PlLlses10funCTY=function(CLC,t0,cty){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    t1=pmin(t,10+t0)
    t2=pmax(10+t0,pmin(t,20+t0))
    t3=pmax(20+t0,pmin(t,30+t0))
    t4=pmax(30+t0,pmin(t,40+t0))
    t5=pmax(40+t0,pmin(t,50+t0))
    t6=pmax(50+t0,t)
    CI.PlLl[t,1]=CLC$coefficients[paste0("Pl_",cty)]+CLC$coefficients["Pl:time1"]*t1+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6-
      (CLC$coefficients[paste0("Ll_",cty)]+CLC$coefficients["Ll:time1"]*t1+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)[paste0("Pl_",cty),paste0("Pl_",cty)]+t1^2*vcov(CLC)["Pl:time1","Pl:time1"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      1*vcov(CLC)[paste0("Ll_",cty),paste0("Ll_",cty)]+t1^2*vcov(CLC)["Ll:time1","Ll:time1"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]-
      2*1*1*vcov(CLC)[paste0("Pl_",cty),paste0("Ll_",cty)]+2*1*t1*vcov(CLC)[paste0("Pl_",cty),"Pl:time1"]-2*1*t1*vcov(CLC)[paste0("Pl_",cty),"Ll:time1"]+2*1*t2*vcov(CLC)[paste0("Pl_",cty),"Pl:time2"]-2*1*t2*vcov(CLC)[paste0("Pl_",cty),"Ll:time2"]+2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Pl:time3"]-2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Ll:time3"]+
      2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Pl:time4"]-2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Ll:time4"]+2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Pl:time5"]-2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Ll:time5"]+2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Pl:time6"]-2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Ll:time6"]-
      2*1*t1*vcov(CLC)[paste0("Ll_",cty),"Pl:time1"]+2*1*t1*vcov(CLC)[paste0("Ll_",cty),"Ll:time1"]-2*1*t2*vcov(CLC)[paste0("Ll_",cty),"Pl:time2"]+2*1*t2*vcov(CLC)[paste0("Ll_",cty),"Ll:time2"]-2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Pl:time3"]+2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Ll:time3"]-
      2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Pl:time4"]+2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Ll:time4"]-2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Pl:time5"]+2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Ll:time5"]-2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Pl:time6"]+2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Ll:time6"]-
      2*t1*t1*vcov(CLC)["Pl:time1","Ll:time1"]+2*t1*t2*vcov(CLC)["Pl:time1","Pl:time2"]-2*t1*t2*vcov(CLC)["Pl:time1","Ll:time2"]+2*t1*t3*vcov(CLC)["Pl:time1","Pl:time3"]-2*t1*t3*vcov(CLC)["Pl:time1","Ll:time3"]+
      2*t1*t4*vcov(CLC)["Pl:time1","Pl:time4"]-2*t1*t4*vcov(CLC)["Pl:time1","Ll:time4"]+2*t1*t5*vcov(CLC)["Pl:time1","Pl:time5"]-2*t1*t5*vcov(CLC)["Pl:time1","Ll:time5"]+2*t1*t6*vcov(CLC)["Pl:time1","Pl:time6"]-2*t1*t6*vcov(CLC)["Pl:time1","Ll:time6"]-
      2*t1*t2*vcov(CLC)["Ll:time1","Pl:time2"]+2*t1*t2*vcov(CLC)["Ll:time1","Ll:time2"]-2*t1*t3*vcov(CLC)["Ll:time1","Pl:time3"]+2*t1*t3*vcov(CLC)["Ll:time1","Ll:time3"]-
      2*t1*t4*vcov(CLC)["Ll:time1","Pl:time4"]+2*t1*t4*vcov(CLC)["Ll:time1","Ll:time4"]-2*t1*t5*vcov(CLC)["Ll:time1","Pl:time5"]+2*t1*t5*vcov(CLC)["Ll:time1","Ll:time5"]-2*t1*t6*vcov(CLC)["Ll:time1","Pl:time6"]+2*t1*t6*vcov(CLC)["Ll:time1","Ll:time6"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  CI.PlLl
}

#Z-test functions:
ZtestfunS=function(CLC,per){
  m=CLC$coefficients[paste0("Pl:time",per)]-CLC$coefficients[paste0("Ll:time",per)]#mean
  v=vcov(CLC)[paste0("Pl:time",per),paste0("Pl:time",per)]+vcov(CLC)[paste0("Ll:time",per),paste0("Ll:time",per)]-
    2*vcov(CLC)[paste0("Pl:time",per),paste0("Ll:time",per)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
de$time1=pmin(de$time,10+t0)
de$time2=pmax(10+t0,pmin(de$time,20+t0))
de$time3=pmax(20+t0,pmin(de$time,30+t0))
de$time4=pmax(30+t0,pmin(de$time,40+t0))
de$time5=pmax(40+t0,pmin(de$time,50+t0))
de$time6=pmax(50+t0,de$time)

#regressions:
CLC_ses10_0=clogit(Va ~ Pl:time1+Ll:time1+Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6
                      +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                      +strata(Esalt) ,robust=T ,data=de, method="efron")#regression
names(CLC_ses10_0$coefficients)[names(CLC_ses10_0$coefficients)=="time1:Ll"]="Ll:time1"


##Table C2##
#table (hypothesis testing):
stargazer(CLC_ses10_0,no.space = T,single.row = T)
ZtestfunS(CLC_ses10_0,1)
ZtestfunS(CLC_ses10_0,2)
ZtestfunS(CLC_ses10_0,3)
ZtestfunS(CLC_ses10_0,4)
ZtestfunS(CLC_ses10_0,5)
ZtestfunS(CLC_ses10_0,6)
#list and test countries by average variance:
vaCLC_ses10_0=matrix(NA,nrow=length(sort(unique(de$Ec))),ncol=2)#average variance by country over time
for(i in 1:length(sort(unique(de$Ec)))) {#get average variance over time
  vaCLC_ses10_0[i,1]=paste0(sort(unique(de$Ec))[i])
  vaCLC_ses10_0[i,2]=mean(CI.PlLlses10funCTY(CLC_ses10_0,0,sort(unique(de$Ec))[i])[,2],na.rm=T)
}
vaCLC_ses10_0=as.data.frame(vaCLC_ses10_0)
vaCLC_ses10_0=vaCLC_ses10_0[order(vaCLC_ses10_0$V2),]
vaCLC_ses10_0[9,1]#country with median variance
vaCLC_ses10_0[,1]#list of countries by variance (small to high)#virtually the same than two periods
#plots (median cty):
# tikz(paste0("plot_","CLC_ses10_0","_PlLl.tex"),width=4, height=3)#plot Pl-Ll:
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLlses10funCTY(CLC_ses10_0,0,sort(unique(de$Ec))[11])) )
# dev.off()


##Table I12##
##spline table:
stargazer(CLC_ses,CLC_sess,CLC_sesss,CLC_sessss,CLC_ses10_0)


#THEORY 10Y SEGMENTS - LEFT VOTERS:----

#confidence interval functions:
CI.PlLllses10LRfunCTY=function(CLC,t0,pe,cty){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=8)#Pl:
  for (t in 1:59) {
    t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.PlLl[t,1]=CLC$coefficients[paste0("Pl_",cty)]+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6+
      CLC$coefficients["Pl:time2:DL"]*t2+CLC$coefficients["Pl:time3:DL"]*t3+CLC$coefficients["Pl:time4:DL"]*t4+CLC$coefficients["Pl:time5:DL"]*t5+CLC$coefficients["Pl:time6:DL"]*t6-
      (CLC$coefficients[paste0("Ll_",cty)]+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6+
         CLC$coefficients["Ll:time2:DL"]*t2+CLC$coefficients["Ll:time3:DL"]*t3+CLC$coefficients["Ll:time4:DL"]*t4+CLC$coefficients["Ll:time5:DL"]*t5+CLC$coefficients["Ll:time6:DL"]*t6)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)[paste0("Pl_",cty),paste0("Pl_",cty)]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      t2^2*vcov(CLC)["Pl:time2:DL","Pl:time2:DL"]+t3^2*vcov(CLC)["Pl:time3:DL","Pl:time3:DL"]+t4^2*vcov(CLC)["Pl:time4:DL","Pl:time4:DL"]+t5^2*vcov(CLC)["Pl:time5:DL","Pl:time5:DL"]+t6^2*vcov(CLC)["Pl:time6:DL","Pl:time6:DL"]+
      1*vcov(CLC)[paste0("Ll_",cty),paste0("Ll_",cty)]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      t2^2*vcov(CLC)["Ll:time2:DL","Ll:time2:DL"]+t3^2*vcov(CLC)["Ll:time3:DL","Ll:time3:DL"]+t4^2*vcov(CLC)["Ll:time4:DL","Ll:time4:DL"]+t5^2*vcov(CLC)["Ll:time5:DL","Ll:time5:DL"]+t6^2*vcov(CLC)["Ll:time6:DL","Ll:time6:DL"]-
      2*1*1*vcov(CLC)[paste0("Pl_",cty),paste0("Ll_",cty)]+2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Pl:time3"]-2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Ll:time3"]+
      2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Pl:time4"]-2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Ll:time4"]+2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Pl:time5"]-2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Ll:time5"]+2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Pl:time6"]-2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Ll:time6"]+
      2*1*t2*vcov(CLC)[paste0("Pl_",cty),"Pl:time2:DL"]-2*1*t2*vcov(CLC)[paste0("Pl_",cty),"Ll:time2:DL"]+2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Pl:time3:DL"]-2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Ll:time3:DL"]+
      2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Pl:time4:DL"]-2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Ll:time4:DL"]+2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Pl:time5:DL"]-2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Ll:time5:DL"]+2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Pl:time6:DL"]-2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Ll:time6:DL"]-
      2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Pl:time3"]+2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Ll:time3"]-
      2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Pl:time4"]+2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Ll:time4"]-2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Pl:time5"]+2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Ll:time5"]-2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Pl:time6"]+2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Ll:time6"]-
      2*1*t2*vcov(CLC)[paste0("Ll_",cty),"Pl:time2:DL"]+2*1*t2*vcov(CLC)[paste0("Ll_",cty),"Ll:time2:DL"]-2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Pl:time3:DL"]+2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Ll:time3:DL"]-
      2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Pl:time4:DL"]+2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Ll:time4:DL"]-2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Pl:time5:DL"]+2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Ll:time5:DL"]-2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Pl:time6:DL"]+2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]+
      2*t3*t2*vcov(CLC)["Pl:time3","Pl:time2:DL"]-2*t3*t2*vcov(CLC)["Pl:time3","Ll:time2:DL"]+2*t3*t3*vcov(CLC)["Pl:time3","Pl:time3:DL"]-2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t3*t2*vcov(CLC)["Ll:time3","Pl:time2:DL"]+2*t3*t2*vcov(CLC)["Ll:time3","Ll:time2:DL"]-2*t3*t3*vcov(CLC)["Ll:time3","Pl:time3:DL"]+2*t3*t3*vcov(CLC)["Ll:time3","Ll:time3:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]+
      2*t4*t2*vcov(CLC)["Pl:time4","Pl:time2:DL"]-2*t4*t2*vcov(CLC)["Pl:time4","Ll:time2:DL"]+2*t4*t3*vcov(CLC)["Pl:time4","Pl:time3:DL"]-2*t4*t3*vcov(CLC)["Pl:time4","Ll:time3:DL"]+
      2*t4*t4*vcov(CLC)["Pl:time4","Pl:time4:DL"]-2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t4*t2*vcov(CLC)["Ll:time4","Pl:time2:DL"]+2*t4*t2*vcov(CLC)["Ll:time4","Ll:time2:DL"]-2*t4*t3*vcov(CLC)["Ll:time4","Pl:time3:DL"]+2*t4*t3*vcov(CLC)["Ll:time4","Ll:time3:DL"]-
      2*t4*t4*vcov(CLC)["Ll:time4","Pl:time4:DL"]+2*t4*t4*vcov(CLC)["Ll:time4","Ll:time4:DL"]-2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]+
      2*t5*t2*vcov(CLC)["Pl:time5","Pl:time2:DL"]-2*t5*t2*vcov(CLC)["Pl:time5","Ll:time2:DL"]+2*t5*t3*vcov(CLC)["Pl:time5","Pl:time3:DL"]-2*t5*t3*vcov(CLC)["Pl:time5","Ll:time3:DL"]+
      2*t5*t4*vcov(CLC)["Pl:time5","Pl:time4:DL"]-2*t5*t4*vcov(CLC)["Pl:time5","Ll:time4:DL"]+2*t5*t5*vcov(CLC)["Pl:time5","Pl:time5:DL"]-2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t5*t2*vcov(CLC)["Ll:time5","Pl:time2:DL"]+2*t5*t2*vcov(CLC)["Ll:time5","Ll:time2:DL"]-2*t5*t3*vcov(CLC)["Ll:time5","Pl:time3:DL"]+2*t5*t3*vcov(CLC)["Ll:time5","Ll:time3:DL"]-
      2*t5*t4*vcov(CLC)["Ll:time5","Pl:time4:DL"]+2*t5*t4*vcov(CLC)["Ll:time5","Ll:time4:DL"]-2*t5*t5*vcov(CLC)["Ll:time5","Pl:time5:DL"]+2*t5*t5*vcov(CLC)["Ll:time5","Ll:time5:DL"]-2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]+
      2*t6*t2*vcov(CLC)["Pl:time6","Pl:time2:DL"]-2*t6*t2*vcov(CLC)["Pl:time6","Ll:time2:DL"]+2*t6*t3*vcov(CLC)["Pl:time6","Pl:time3:DL"]-2*t6*t3*vcov(CLC)["Pl:time6","Ll:time3:DL"]+
      2*t6*t4*vcov(CLC)["Pl:time6","Pl:time4:DL"]-2*t6*t4*vcov(CLC)["Pl:time6","Ll:time4:DL"]+2*t6*t5*vcov(CLC)["Pl:time6","Pl:time5:DL"]-2*t6*t5*vcov(CLC)["Pl:time6","Ll:time5:DL"]+2*t6*t6*vcov(CLC)["Pl:time6","Pl:time6:DL"]-2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6:DL"]-
      2*t6*t2*vcov(CLC)["Ll:time6","Pl:time2:DL"]+2*t6*t2*vcov(CLC)["Ll:time6","Ll:time2:DL"]-2*t6*t3*vcov(CLC)["Ll:time6","Pl:time3:DL"]+2*t6*t3*vcov(CLC)["Ll:time6","Ll:time3:DL"]-
      2*t6*t4*vcov(CLC)["Ll:time6","Pl:time4:DL"]+2*t6*t4*vcov(CLC)["Ll:time6","Ll:time4:DL"]-2*t6*t5*vcov(CLC)["Ll:time6","Pl:time5:DL"]+2*t6*t5*vcov(CLC)["Ll:time6","Ll:time5:DL"]-2*t6*t6*vcov(CLC)["Ll:time6","Pl:time6:DL"]+2*t6*t6*vcov(CLC)["Ll:time6","Ll:time6:DL"]-
      2*t2*t2*vcov(CLC)["Pl:time2:DL","Ll:time2:DL"]+2*t2*t3*vcov(CLC)["Pl:time2:DL","Pl:time3:DL"]-2*t2*t3*vcov(CLC)["Pl:time2:DL","Ll:time3:DL"]+
      2*t2*t4*vcov(CLC)["Pl:time2:DL","Pl:time4:DL"]-2*t2*t4*vcov(CLC)["Pl:time2:DL","Ll:time4:DL"]+2*t2*t5*vcov(CLC)["Pl:time2:DL","Pl:time5:DL"]-2*t2*t5*vcov(CLC)["Pl:time2:DL","Ll:time5:DL"]+2*t2*t6*vcov(CLC)["Pl:time2:DL","Pl:time6:DL"]-2*t2*t6*vcov(CLC)["Pl:time2:DL","Ll:time6:DL"]-
      2*t2*t3*vcov(CLC)["Ll:time2:DL","Pl:time3:DL"]+2*t2*t3*vcov(CLC)["Ll:time2:DL","Ll:time3:DL"]-
      2*t2*t4*vcov(CLC)["Ll:time2:DL","Pl:time4:DL"]+2*t2*t4*vcov(CLC)["Ll:time2:DL","Ll:time4:DL"]-2*t2*t5*vcov(CLC)["Ll:time2:DL","Pl:time5:DL"]+2*t2*t5*vcov(CLC)["Ll:time2:DL","Ll:time5:DL"]-2*t2*t6*vcov(CLC)["Ll:time2:DL","Pl:time6:DL"]+2*t2*t6*vcov(CLC)["Ll:time2:DL","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3:DL","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3:DL","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3:DL","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3:DL","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3:DL","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3:DL","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3:DL","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3:DL","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3:DL","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3:DL","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3:DL","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3:DL","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3:DL","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4:DL","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4:DL","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4:DL","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4:DL","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4:DL","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4:DL","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4:DL","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4:DL","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4:DL","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5:DL","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5:DL","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5:DL","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5:DL","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5:DL","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6:DL","Ll:time6:DL"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
    #right:
    CI.PlLl[t,5]=CLC$coefficients[paste0("Pl_",cty)]+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6-
      (CLC$coefficients[paste0("Ll_",cty)]+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6)#fitted values
    CI.PlLl[t,6]=1*vcov(CLC)[paste0("Pl_",cty),paste0("Pl_",cty)]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      1*vcov(CLC)[paste0("Ll_",cty),paste0("Ll_",cty)]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]-
      2*1*1*vcov(CLC)[paste0("Pl_",cty),paste0("Ll_",cty)]+2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Pl:time3"]-2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Ll:time3"]+
      2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Pl:time4"]-2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Ll:time4"]+2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Pl:time5"]-2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Ll:time5"]+2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Pl:time6"]-2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Ll:time6"]-
      2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Pl:time3"]+2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Ll:time3"]-
      2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Pl:time4"]+2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Ll:time4"]-2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Pl:time5"]+2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Ll:time5"]-2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Pl:time6"]+2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Ll:time6"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]
    CI.PlLl[t,7]=CI.PlLl[t,5]-qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI lower
    CI.PlLl[t,8]=CI.PlLl[t,5]+qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}

#Z-test functions:
ZtestfunT=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTD=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1,":DL")]-CLC$coefficients[paste0("Ll:time",per1,":DL")]-
        CLC$coefficients[paste0("Pl:time",per2,":DL")]+CLC$coefficients[paste0("Ll:time",per2,":DL")])#mean
  v=vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per1,":DL")]+vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per1,":DL")]+
    vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Pl:time",per2,":DL")]+vcov(CLC)[paste0("Ll:time",per2,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2,":DL")]-2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Ll:time",per2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunD=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]+
        CLC$coefficients[paste0("Pl:time",per1,":DL")]-CLC$coefficients[paste0("Ll:time",per1,":DL")]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)]-
        CLC$coefficients[paste0("Pl:time",per2,":DL")]+CLC$coefficients[paste0("Ll:time",per2,":DL")])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per1,":DL")]+vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per1,":DL")]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]+
    vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Pl:time",per2,":DL")]+vcov(CLC)[paste0("Ll:time",per2,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]+2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per1,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1,":DL")]+2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2)]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2)]-
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per2),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Ll:time",per2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}


##left-right - 0.25 quantile:
de=de %>% group_by(Es) %>% mutate(LRR25 = quantile(LRR,0.25,na.rm=TRUE))#0.25 quantile by Es (left)
de$DL=ifelse(de$LRR<de$LRR25,1,0)# left # population 1/4,3/4
de$DR=ifelse(de$LRR>=de$LRR25,1,0)# right

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regressions:
CLC_lses10_LR25_0=clogit(Va ~ Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                           Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                           +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                         +strata(Esalt), robust=T, data=de[!is.na(de$LRR),], method="efron")#regression
names(CLC_lses10_LR25_0$coefficients)[names(CLC_lses10_LR25_0$coefficients)=="time3:Ll"]="Ll:time3"
names(CLC_lses10_LR25_0$coefficients)[names(CLC_lses10_LR25_0$coefficients)=="time3:Ll:DL"]="Ll:time3:DL"


##Table 6##
#hypothesis testing:
stargazer(CLC_lses10_LR25_0,no.space = T,single.row = T)
#drop left-quartile voters:
ZtestfunD(CLC_lses10_LR25_0,3,4)
ZtestfunD(CLC_lses10_LR25_0,4,5)
ZtestfunD(CLC_lses10_LR25_0,5,6)
#drop remaining voters:
ZtestfunT(CLC_lses10_LR25_0,3,4)
ZtestfunT(CLC_lses10_LR25_0,4,5)
ZtestfunT(CLC_lses10_LR25_0,5,6)
#difference in drop between left-quartile and remaining voters:
ZtestfunTD(CLC_lses10_LR25_0,3,4)
ZtestfunTD(CLC_lses10_LR25_0,4,5)
ZtestfunTD(CLC_lses10_LR25_0,5,6)
#list and test countries by average variance:
vaCLC_lses10_LR25_0=matrix(NA,nrow=length(sort(unique(de$Ec))),ncol=2)#average variance by country over time
for(i in 1:length(sort(unique(de$Ec)))) {#get average variance over time
  vaCLC_lses10_LR25_0[i,1]=paste0(sort(unique(de$Ec))[i])
  vaCLC_lses10_LR25_0[i,2]=mean(CI.PlLllses10LRfunCTY(CLC_lses10_LR25_0,0,4,sort(unique(de$Ec))[i])[,2],na.rm=T)
}
vaCLC_lses10_LR25_0=as.data.frame(vaCLC_lses10_LR25_0)
vaCLC_lses10_LR25_0=vaCLC_lses10_LR25_0[order(vaCLC_lses10_LR25_0$V2),]
vaCLC_lses10_LR25_0[9,1]#country with median variance (the same considering each period)
vaCLC_lses10_LR25_0[,1]#list of countries by variance (small to high)#virtually the same than two periods
#NB: here ISL is not more the median, but I keep it to ave same country for all graphs
#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_LR25_0","_PlLl_ctymed.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyLRR) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_0,0,3,sort(unique(de$Ec))[11])) +#left
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_0,0,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_0,0,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_0,0,6,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_0,0,3,sort(unique(de$Ec))[11])) +#right
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_0,0,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_0,0,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_0,0,6,sort(unique(de$Ec))[11])) )
# dev.off()


##left-right - 0.15 quantile:
de=de %>% group_by(Es) %>% mutate(LRR15 = quantile(LRR,0.15,na.rm=TRUE))#0.333 quantile by Es (left)
de$DL=ifelse(de$LRR<de$LRR15,1,0)# left # population 1/3,2/3
de$DR=ifelse(de$LRR>=de$LRR15,1,0)# right
#regressions:
CLC_lses10_LR15_0=clogit(Va ~ Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                           Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                           +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                         +strata(Esalt), robust=T, data=de[!is.na(de$LRR),], method="efron")#regression
names(CLC_lses10_LR15_0$coefficients)[names(CLC_lses10_LR15_0$coefficients)=="time3:Ll"]="Ll:time3"
names(CLC_lses10_LR15_0$coefficients)[names(CLC_lses10_LR15_0$coefficients)=="time3:Ll:DL"]="Ll:time3:DL"


##Table G1##
#hypothesis testing:
stargazer(CLC_lses10_LR15_0,no.space = T,single.row = T)
#drop left-quartile voters:
ZtestfunD(CLC_lses10_LR15_0,3,4)
ZtestfunD(CLC_lses10_LR15_0,4,5)
ZtestfunD(CLC_lses10_LR15_0,5,6)
#drop remaining voters:
ZtestfunT(CLC_lses10_LR15_0,3,4)
ZtestfunT(CLC_lses10_LR15_0,4,5)
ZtestfunT(CLC_lses10_LR15_0,5,6)
#difference in drop between left-quartile and remaining voters:
ZtestfunTD(CLC_lses10_LR15_0,3,4)
ZtestfunTD(CLC_lses10_LR15_0,4,5)
ZtestfunTD(CLC_lses10_LR15_0,5,6)
#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_LR15_0","_PlLl_ctymed.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyLRR) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR15_0,0,3,sort(unique(de$Ec))[11])) +#left
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR15_0,0,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR15_0,0,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR15_0,0,6,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR15_0,0,3,sort(unique(de$Ec))[11])) +#right
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR15_0,0,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR15_0,0,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR15_0,0,6,sort(unique(de$Ec))[11])) )
# dev.off()


##left-right - 0.35 quantile:
de=de %>% group_by(Es) %>% mutate(LRR35 = quantile(LRR,0.35,na.rm=TRUE))#0.333 quantile by Es (left)
de$DL=ifelse(de$LRR<de$LRR35,1,0)# left # population 1/3,2/3
de$DR=ifelse(de$LRR>=de$LRR35,1,0)# right
#regressions:
CLC_lses10_LR35_0=clogit(Va ~ Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                           Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                           +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                         +strata(Esalt), robust=T, data=de[!is.na(de$LRR),], method="efron")#regression
names(CLC_lses10_LR35_0$coefficients)[names(CLC_lses10_LR35_0$coefficients)=="time3:Ll"]="Ll:time3"
names(CLC_lses10_LR35_0$coefficients)[names(CLC_lses10_LR35_0$coefficients)=="time3:Ll:DL"]="Ll:time3:DL"


##Table G2##
#hypothesis testing:
stargazer(CLC_lses10_LR35_0,no.space = T,single.row = T)
#drop left-quartile voters:
ZtestfunD(CLC_lses10_LR35_0,3,4)
ZtestfunD(CLC_lses10_LR35_0,4,5)
ZtestfunD(CLC_lses10_LR35_0,5,6)
#drop remaining voters:
ZtestfunT(CLC_lses10_LR35_0,3,4)
ZtestfunT(CLC_lses10_LR35_0,4,5)
ZtestfunT(CLC_lses10_LR35_0,5,6)
#difference in drop between left-quartile and remaining voters:
ZtestfunTD(CLC_lses10_LR35_0,3,4)
ZtestfunTD(CLC_lses10_LR35_0,4,5)
ZtestfunTD(CLC_lses10_LR35_0,5,6)
#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_LR35_0","_PlLl_ctymed.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.35,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyLRR) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR35_0,0,3,sort(unique(de$Ec))[11])) +#left
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR35_0,0,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR35_0,0,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR35_0,0,6,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR35_0,0,3,sort(unique(de$Ec))[11])) +#right
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR35_0,0,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR35_0,0,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR35_0,0,6,sort(unique(de$Ec))[11])) )
# dev.off()


##Table I15##
##table full results:
stargazer(CLC_lses10_LR15_0,CLC_lses10_LR35_0)


##THEORY 10Y SEGMENTS - LEFT VOTERS - ALTERNATIVE THRESHOLDS AND FULL REUSLTS TABLE:----

##left-right - 0.25 quantile:
de=de %>% group_by(Es) %>% mutate(LRR25 = quantile(LRR,0.25,na.rm=TRUE))#0.25 quantile by Es (left)
de$DL=ifelse(de$LRR<de$LRR25,1,0)# left # population 1/4,3/4
de$DR=ifelse(de$LRR>=de$LRR25,1,0)# right


##Table D9##
##starting value for thresholds -4
#time variables:
t0=-4#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regressions:
CLC_lses10_LR25_m4=clogit(Va ~ Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                           Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                           +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                         +strata(Esalt), robust=T, data=de[!is.na(de$LRR),], method="efron")#regression
names(CLC_lses10_LR25_m4$coefficients)[names(CLC_lses10_LR25_m4$coefficients)=="time3:Ll"]="Ll:time3"
names(CLC_lses10_LR25_m4$coefficients)[names(CLC_lses10_LR25_m4$coefficients)=="time3:Ll:DL"]="Ll:time3:DL"

#hypothesis testing:
stargazer(CLC_lses10_LR25_m4,no.space = T,single.row = T)
#drop left-quartile voters:
ZtestfunD(CLC_lses10_LR25_m4,3,4)
ZtestfunD(CLC_lses10_LR25_m4,4,5)
ZtestfunD(CLC_lses10_LR25_m4,5,6)
#drop remaining voters:
ZtestfunT(CLC_lses10_LR25_m4,3,4)
ZtestfunT(CLC_lses10_LR25_m4,4,5)
ZtestfunT(CLC_lses10_LR25_m4,5,6)
#difference in drop between left-quartile and remaining voters:
ZtestfunTD(CLC_lses10_LR25_m4,3,4)
ZtestfunTD(CLC_lses10_LR25_m4,4,5)
ZtestfunTD(CLC_lses10_LR25_m4,5,6)

#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_LR25_m4","_PlLl_ctymed.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyLRR) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m4,-4,3,sort(unique(de$Ec))[11])) +#left
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m4,-4,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m4,-4,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m4,-4,6,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m4,-4,3,sort(unique(de$Ec))[11])) +#right
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m4,-4,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m4,-4,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m4,-4,6,sort(unique(de$Ec))[11])) )
# dev.off()


##starting value for thresholds -2
#time variables:
t0=-2#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regressions:
CLC_lses10_LR25_m2=clogit(Va ~ Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                            Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                            +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                          +strata(Esalt), robust=T, data=de[!is.na(de$LRR),], method="efron")#regression
names(CLC_lses10_LR25_m2$coefficients)[names(CLC_lses10_LR25_m2$coefficients)=="time3:Ll"]="Ll:time3"
names(CLC_lses10_LR25_m2$coefficients)[names(CLC_lses10_LR25_m2$coefficients)=="time3:Ll:DL"]="Ll:time3:DL"

#hypothesis testing:
stargazer(CLC_lses10_LR25_m2,no.space = T,single.row = T)
#drop left-quartile voters:
ZtestfunD(CLC_lses10_LR25_m2,3,4)
ZtestfunD(CLC_lses10_LR25_m2,4,5)
ZtestfunD(CLC_lses10_LR25_m2,5,6)
#drop remaining voters:
ZtestfunT(CLC_lses10_LR25_m2,3,4)
ZtestfunT(CLC_lses10_LR25_m2,4,5)
ZtestfunT(CLC_lses10_LR25_m2,5,6)
#difference in drop between left-quartile and remaining voters:
ZtestfunTD(CLC_lses10_LR25_m2,3,4)
ZtestfunTD(CLC_lses10_LR25_m2,4,5)
ZtestfunTD(CLC_lses10_LR25_m2,5,6)

#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_LR25_m2","_PlLl_ctymed.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyLRR) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m2,-2,3,sort(unique(de$Ec))[11])) +#left
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m2,-2,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m2,-2,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m2,-2,6,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m2,-2,3,sort(unique(de$Ec))[11])) +#right
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m2,-2,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m2,-2,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_m2,-2,6,sort(unique(de$Ec))[11])) )
# dev.off()


##starting value for thresholds 2
#time variables:
t0=2#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regressions:
CLC_lses10_LR25_2=clogit(Va ~ Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                            Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                            +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                          +strata(Esalt), robust=T, data=de[!is.na(de$LRR),], method="efron")#regression
names(CLC_lses10_LR25_2$coefficients)[names(CLC_lses10_LR25_2$coefficients)=="time3:Ll"]="Ll:time3"
names(CLC_lses10_LR25_2$coefficients)[names(CLC_lses10_LR25_2$coefficients)=="time3:Ll:DL"]="Ll:time3:DL"

#hypothesis testing:
stargazer(CLC_lses10_LR25_2,no.space = T,single.row = T)
#drop left-quartile voters:
ZtestfunD(CLC_lses10_LR25_2,3,4)
ZtestfunD(CLC_lses10_LR25_2,4,5)
ZtestfunD(CLC_lses10_LR25_2,5,6)
#drop remaining voters:
ZtestfunT(CLC_lses10_LR25_2,3,4)
ZtestfunT(CLC_lses10_LR25_2,4,5)
ZtestfunT(CLC_lses10_LR25_2,5,6)
#difference in drop between left-quartile and remaining voters:
ZtestfunTD(CLC_lses10_LR25_2,3,4)
ZtestfunTD(CLC_lses10_LR25_2,4,5)
ZtestfunTD(CLC_lses10_LR25_2,5,6)

#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_LR25_2","_PlLl_ctymed.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyLRR) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_2,2,3,sort(unique(de$Ec))[11])) +#left
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_2,2,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_2,2,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_2,2,6,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_2,2,3,sort(unique(de$Ec))[11])) +#right
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_2,2,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_2,2,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_2,2,6,sort(unique(de$Ec))[11])) )
# dev.off()


##starting value for thresholds 4
#time variables:
t0=4#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regressions:
CLC_lses10_LR25_4=clogit(Va ~ Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                            Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                            +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                          +strata(Esalt), robust=T, data=de[!is.na(de$LRR),], method="efron")#regression
names(CLC_lses10_LR25_4$coefficients)[names(CLC_lses10_LR25_4$coefficients)=="time3:Ll"]="Ll:time3"
names(CLC_lses10_LR25_4$coefficients)[names(CLC_lses10_LR25_4$coefficients)=="time3:Ll:DL"]="Ll:time3:DL"

#hypothesis testing:
stargazer(CLC_lses10_LR25_4,no.space = T,single.row = T)
#drop left-quartile voters:
ZtestfunD(CLC_lses10_LR25_4,3,4)
ZtestfunD(CLC_lses10_LR25_4,4,5)
ZtestfunD(CLC_lses10_LR25_4,5,6)
#drop remaining voters:
ZtestfunT(CLC_lses10_LR25_4,3,4)
ZtestfunT(CLC_lses10_LR25_4,4,5)
ZtestfunT(CLC_lses10_LR25_4,5,6)
#difference in drop between left-quartile and remaining voters:
ZtestfunTD(CLC_lses10_LR25_4,3,4)
ZtestfunTD(CLC_lses10_LR25_4,4,5)
ZtestfunTD(CLC_lses10_LR25_4,5,6)

#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_LR25_4","_PlLl_ctymed.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyLRR) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_4,4,3,sort(unique(de$Ec))[11])) +#left
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_4,4,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_4,4,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_4,4,6,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_4,4,3,sort(unique(de$Ec))[11])) +#right
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_4,4,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_4,4,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses10_LR25_4,4,6,sort(unique(de$Ec))[11])) )
# dev.off()


##Table I7##
##table full results:
cm4=CLC_lses10_LR25_m4#shorten name for stargazer:
cm2=CLC_lses10_LR25_m2
c0=CLC_lses10_LR25_0
c2=CLC_lses10_LR25_2
c4=CLC_lses10_LR25_4
stargazer(cm4,cm2,c0,c2,c4)#table
rm(cm4,cm2,c0,c2,c4)


#THEORY 10Y SEGMENTS - GERMANY:----

#Germany dummy:
de$DL[de$Ec %in% c("DEU")]=1#Germany
de$DL[!de$Ec %in% c("DEU")]=0#others

#confidence interval functions:
CI.PlLllses10CGfun=function(CLC,t0,pe){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=8)#Pl:
  for (t in 1:59) {
    t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    #left:
    CI.PlLl[t,1]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6+
      CLC$coefficients["Pl:time2:DL"]*t2+CLC$coefficients["Pl:time3:DL"]*t3+CLC$coefficients["Pl:time4:DL"]*t4+CLC$coefficients["Pl:time5:DL"]*t5+CLC$coefficients["Pl:time6:DL"]*t6-
      (CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6+
         CLC$coefficients["Ll:time2:DL"]*t2+CLC$coefficients["Ll:time3:DL"]*t3+CLC$coefficients["Ll:time4:DL"]*t4+CLC$coefficients["Ll:time5:DL"]*t5+CLC$coefficients["Ll:time6:DL"]*t6)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      t2^2*vcov(CLC)["Pl:time2:DL","Pl:time2:DL"]+t3^2*vcov(CLC)["Pl:time3:DL","Pl:time3:DL"]+t4^2*vcov(CLC)["Pl:time4:DL","Pl:time4:DL"]+t5^2*vcov(CLC)["Pl:time5:DL","Pl:time5:DL"]+t6^2*vcov(CLC)["Pl:time6:DL","Pl:time6:DL"]+
      1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      t2^2*vcov(CLC)["Ll:time2:DL","Ll:time2:DL"]+t3^2*vcov(CLC)["Ll:time3:DL","Ll:time3:DL"]+t4^2*vcov(CLC)["Ll:time4:DL","Ll:time4:DL"]+t5^2*vcov(CLC)["Ll:time5:DL","Ll:time5:DL"]+t6^2*vcov(CLC)["Ll:time6:DL","Ll:time6:DL"]-
      2*1*1*vcov(CLC)["Pl_ISL","Ll_ISL"]+2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6"]+
      2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2:DL"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2:DL"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3:DL"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3:DL"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4:DL"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4:DL"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5:DL"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5:DL"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6:DL"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6:DL"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2:DL"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2:DL"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3:DL"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3:DL"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4:DL"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4:DL"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5:DL"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5:DL"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6:DL"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6:DL"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]+
      2*t2*t2*vcov(CLC)["Pl:time2","Pl:time2:DL"]-2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2:DL"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3:DL"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3:DL"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4:DL"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4:DL"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5:DL"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5:DL"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6:DL"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6:DL"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t2*t2*vcov(CLC)["Ll:time2","Pl:time2:DL"]+2*t2*t2*vcov(CLC)["Ll:time2","Ll:time2:DL"]-2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3:DL"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3:DL"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4:DL"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4:DL"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5:DL"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5:DL"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6:DL"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]+
      2*t3*t2*vcov(CLC)["Pl:time3","Pl:time2:DL"]-2*t3*t2*vcov(CLC)["Pl:time3","Ll:time2:DL"]+2*t3*t3*vcov(CLC)["Pl:time3","Pl:time3:DL"]-2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t3*t2*vcov(CLC)["Ll:time3","Pl:time2:DL"]+2*t3*t2*vcov(CLC)["Ll:time3","Ll:time2:DL"]-2*t3*t3*vcov(CLC)["Ll:time3","Pl:time3:DL"]+2*t3*t3*vcov(CLC)["Ll:time3","Ll:time3:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]+
      2*t4*t2*vcov(CLC)["Pl:time4","Pl:time2:DL"]-2*t4*t2*vcov(CLC)["Pl:time4","Ll:time2:DL"]+2*t4*t3*vcov(CLC)["Pl:time4","Pl:time3:DL"]-2*t4*t3*vcov(CLC)["Pl:time4","Ll:time3:DL"]+
      2*t4*t4*vcov(CLC)["Pl:time4","Pl:time4:DL"]-2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t4*t2*vcov(CLC)["Ll:time4","Pl:time2:DL"]+2*t4*t2*vcov(CLC)["Ll:time4","Ll:time2:DL"]-2*t4*t3*vcov(CLC)["Ll:time4","Pl:time3:DL"]+2*t4*t3*vcov(CLC)["Ll:time4","Ll:time3:DL"]-
      2*t4*t4*vcov(CLC)["Ll:time4","Pl:time4:DL"]+2*t4*t4*vcov(CLC)["Ll:time4","Ll:time4:DL"]-2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]+
      2*t5*t2*vcov(CLC)["Pl:time5","Pl:time2:DL"]-2*t5*t2*vcov(CLC)["Pl:time5","Ll:time2:DL"]+2*t5*t3*vcov(CLC)["Pl:time5","Pl:time3:DL"]-2*t5*t3*vcov(CLC)["Pl:time5","Ll:time3:DL"]+
      2*t5*t4*vcov(CLC)["Pl:time5","Pl:time4:DL"]-2*t5*t4*vcov(CLC)["Pl:time5","Ll:time4:DL"]+2*t5*t5*vcov(CLC)["Pl:time5","Pl:time5:DL"]-2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t5*t2*vcov(CLC)["Ll:time5","Pl:time2:DL"]+2*t5*t2*vcov(CLC)["Ll:time5","Ll:time2:DL"]-2*t5*t3*vcov(CLC)["Ll:time5","Pl:time3:DL"]+2*t5*t3*vcov(CLC)["Ll:time5","Ll:time3:DL"]-
      2*t5*t4*vcov(CLC)["Ll:time5","Pl:time4:DL"]+2*t5*t4*vcov(CLC)["Ll:time5","Ll:time4:DL"]-2*t5*t5*vcov(CLC)["Ll:time5","Pl:time5:DL"]+2*t5*t5*vcov(CLC)["Ll:time5","Ll:time5:DL"]-2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]+
      2*t6*t2*vcov(CLC)["Pl:time6","Pl:time2:DL"]-2*t6*t2*vcov(CLC)["Pl:time6","Ll:time2:DL"]+2*t6*t3*vcov(CLC)["Pl:time6","Pl:time3:DL"]-2*t6*t3*vcov(CLC)["Pl:time6","Ll:time3:DL"]+
      2*t6*t4*vcov(CLC)["Pl:time6","Pl:time4:DL"]-2*t6*t4*vcov(CLC)["Pl:time6","Ll:time4:DL"]+2*t6*t5*vcov(CLC)["Pl:time6","Pl:time5:DL"]-2*t6*t5*vcov(CLC)["Pl:time6","Ll:time5:DL"]+2*t6*t6*vcov(CLC)["Pl:time6","Pl:time6:DL"]-2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6:DL"]-
      2*t6*t2*vcov(CLC)["Ll:time6","Pl:time2:DL"]+2*t6*t2*vcov(CLC)["Ll:time6","Ll:time2:DL"]-2*t6*t3*vcov(CLC)["Ll:time6","Pl:time3:DL"]+2*t6*t3*vcov(CLC)["Ll:time6","Ll:time3:DL"]-
      2*t6*t4*vcov(CLC)["Ll:time6","Pl:time4:DL"]+2*t6*t4*vcov(CLC)["Ll:time6","Ll:time4:DL"]-2*t6*t5*vcov(CLC)["Ll:time6","Pl:time5:DL"]+2*t6*t5*vcov(CLC)["Ll:time6","Ll:time5:DL"]-2*t6*t6*vcov(CLC)["Ll:time6","Pl:time6:DL"]+2*t6*t6*vcov(CLC)["Ll:time6","Ll:time6:DL"]-
      2*t2*t2*vcov(CLC)["Pl:time2:DL","Ll:time2:DL"]+2*t2*t3*vcov(CLC)["Pl:time2:DL","Pl:time3:DL"]-2*t2*t3*vcov(CLC)["Pl:time2:DL","Ll:time3:DL"]+
      2*t2*t4*vcov(CLC)["Pl:time2:DL","Pl:time4:DL"]-2*t2*t4*vcov(CLC)["Pl:time2:DL","Ll:time4:DL"]+2*t2*t5*vcov(CLC)["Pl:time2:DL","Pl:time5:DL"]-2*t2*t5*vcov(CLC)["Pl:time2:DL","Ll:time5:DL"]+2*t2*t6*vcov(CLC)["Pl:time2:DL","Pl:time6:DL"]-2*t2*t6*vcov(CLC)["Pl:time2:DL","Ll:time6:DL"]-
      2*t2*t3*vcov(CLC)["Ll:time2:DL","Pl:time3:DL"]+2*t2*t3*vcov(CLC)["Ll:time2:DL","Ll:time3:DL"]-
      2*t2*t4*vcov(CLC)["Ll:time2:DL","Pl:time4:DL"]+2*t2*t4*vcov(CLC)["Ll:time2:DL","Ll:time4:DL"]-2*t2*t5*vcov(CLC)["Ll:time2:DL","Pl:time5:DL"]+2*t2*t5*vcov(CLC)["Ll:time2:DL","Ll:time5:DL"]-2*t2*t6*vcov(CLC)["Ll:time2:DL","Pl:time6:DL"]+2*t2*t6*vcov(CLC)["Ll:time2:DL","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3:DL","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3:DL","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3:DL","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3:DL","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3:DL","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3:DL","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3:DL","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3:DL","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3:DL","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3:DL","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3:DL","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3:DL","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3:DL","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4:DL","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4:DL","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4:DL","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4:DL","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4:DL","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4:DL","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4:DL","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4:DL","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4:DL","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5:DL","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5:DL","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5:DL","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5:DL","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5:DL","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6:DL","Ll:time6:DL"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
    #right:
    CI.PlLl[t,5]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6-
      (CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6)#fitted values
    CI.PlLl[t,6]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]-
      2*1*1*vcov(CLC)["Pl_ISL","Ll_ISL"]+2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]
    CI.PlLl[t,7]=CI.PlLl[t,5]-qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI lower
    CI.PlLl[t,8]=CI.PlLl[t,5]+qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}#wo Pl:time1:DL,Ll:time1:DL

#Z-test functions:
ZtestfunT12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTD12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2,":DL")]-CLC$coefficients[paste0("Ll:time",2,":DL")]#mean
  v=vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Pl:time",2,":DL")]+vcov(CLC)[paste0("Ll:time",2,":DL"),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Ll:time",2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunD12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]+
    CLC$coefficients[paste0("Pl:time",2,":DL")]-CLC$coefficients[paste0("Ll:time",2,":DL")]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]+
    vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Pl:time",2,":DL")]+vcov(CLC)[paste0("Ll:time",2,":DL"),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]+2*vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",2),paste0("Pl:time",2,":DL")]+2*vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Ll:time",2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunT=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTD=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1,":DL")]-CLC$coefficients[paste0("Ll:time",per1,":DL")]-
        CLC$coefficients[paste0("Pl:time",per2,":DL")]+CLC$coefficients[paste0("Ll:time",per2,":DL")])#mean
  v=vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per1,":DL")]+vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per1,":DL")]+
    vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Pl:time",per2,":DL")]+vcov(CLC)[paste0("Ll:time",per2,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2,":DL")]-2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Ll:time",per2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunD=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]+
        CLC$coefficients[paste0("Pl:time",per1,":DL")]-CLC$coefficients[paste0("Ll:time",per1,":DL")]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)]-
        CLC$coefficients[paste0("Pl:time",per2,":DL")]+CLC$coefficients[paste0("Ll:time",per2,":DL")])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per1,":DL")]+vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per1,":DL")]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]+
    vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Pl:time",per2,":DL")]+vcov(CLC)[paste0("Ll:time",per2,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]+2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per1,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1,":DL")]+2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2)]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2)]-
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per2),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Ll:time",per2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regression:
sort(unique(de$Ey[de$DL==1]))#full coverage DL, hence full interactions
sort(unique(de$Ey[de$DL==0]))#full coverage non DL, hence full interactions

CLC_lses10_DEU=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                        Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                        +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                      +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses10_DEU$coefficients)[names(CLC_lses10_DEU$coefficients)=="time2:Ll"]="Ll:time2"
names(CLC_lses10_DEU$coefficients)[names(CLC_lses10_DEU$coefficients)=="time2:Ll:DL"]="Ll:time2:DL"


##Table 7##
#hypothesis testing:
stargazer(CLC_lses10_DEU,no.space = T,single.row = T)
#drop others (DL==0):
ZtestfunT12(CLC_lses10_DEU)
ZtestfunT(CLC_lses10_DEU,2,3)
ZtestfunT(CLC_lses10_DEU,3,4)
ZtestfunT(CLC_lses10_DEU,4,5)
ZtestfunT(CLC_lses10_DEU,5,6)
#drop Germany (DL==1):
ZtestfunD12(CLC_lses10_DEU)
ZtestfunD(CLC_lses10_DEU,2,3)
ZtestfunD(CLC_lses10_DEU,3,4)
ZtestfunD(CLC_lses10_DEU,4,5)
ZtestfunD(CLC_lses10_DEU,5,6)
#difference in drop between Germany and others (DL==1 vs DL==0):
ZtestfunTD12(CLC_lses10_DEU)
ZtestfunTD(CLC_lses10_DEU,2,3)
ZtestfunTD(CLC_lses10_DEU,3,4)
ZtestfunTD(CLC_lses10_DEU,4,5)
ZtestfunTD(CLC_lses10_DEU,5,6)
#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_DDEU.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.2001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.2001)+0.2001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU,0,1)) +#(high)
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU,0,6)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU,0,1)) +#(low)
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU,0,2)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU,0,3)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU,0,4)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU,0,5)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU,0,6)) )
# dev.off()


##THEORY 10Y SEGMENTS - GERMANY - ALTERNATIVE THRESHOLDS AND FULL REUSLTS TABLE:----

#Germany dummy:
de$DL[de$Ec %in% c("DEU")]=1#Germany
de$DL[!de$Ec %in% c("DEU")]=0#others

#confidence interval functions:
CI.PlLllses10CGfun=function(CLC,t0,pe){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=8)#Pl:
  for (t in 1:59) {
    t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    #left:
    CI.PlLl[t,1]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6+
      CLC$coefficients["Pl:time2:DL"]*t2+CLC$coefficients["Pl:time3:DL"]*t3+CLC$coefficients["Pl:time4:DL"]*t4+CLC$coefficients["Pl:time5:DL"]*t5+CLC$coefficients["Pl:time6:DL"]*t6-
      (CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6+
         CLC$coefficients["Ll:time2:DL"]*t2+CLC$coefficients["Ll:time3:DL"]*t3+CLC$coefficients["Ll:time4:DL"]*t4+CLC$coefficients["Ll:time5:DL"]*t5+CLC$coefficients["Ll:time6:DL"]*t6)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      t2^2*vcov(CLC)["Pl:time2:DL","Pl:time2:DL"]+t3^2*vcov(CLC)["Pl:time3:DL","Pl:time3:DL"]+t4^2*vcov(CLC)["Pl:time4:DL","Pl:time4:DL"]+t5^2*vcov(CLC)["Pl:time5:DL","Pl:time5:DL"]+t6^2*vcov(CLC)["Pl:time6:DL","Pl:time6:DL"]+
      1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      t2^2*vcov(CLC)["Ll:time2:DL","Ll:time2:DL"]+t3^2*vcov(CLC)["Ll:time3:DL","Ll:time3:DL"]+t4^2*vcov(CLC)["Ll:time4:DL","Ll:time4:DL"]+t5^2*vcov(CLC)["Ll:time5:DL","Ll:time5:DL"]+t6^2*vcov(CLC)["Ll:time6:DL","Ll:time6:DL"]-
      2*1*1*vcov(CLC)["Pl_ISL","Ll_ISL"]+2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6"]+
      2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2:DL"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2:DL"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3:DL"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3:DL"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4:DL"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4:DL"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5:DL"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5:DL"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6:DL"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6:DL"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2:DL"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2:DL"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3:DL"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3:DL"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4:DL"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4:DL"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5:DL"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5:DL"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6:DL"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6:DL"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]+
      2*t2*t2*vcov(CLC)["Pl:time2","Pl:time2:DL"]-2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2:DL"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3:DL"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3:DL"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4:DL"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4:DL"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5:DL"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5:DL"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6:DL"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6:DL"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t2*t2*vcov(CLC)["Ll:time2","Pl:time2:DL"]+2*t2*t2*vcov(CLC)["Ll:time2","Ll:time2:DL"]-2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3:DL"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3:DL"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4:DL"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4:DL"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5:DL"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5:DL"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6:DL"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]+
      2*t3*t2*vcov(CLC)["Pl:time3","Pl:time2:DL"]-2*t3*t2*vcov(CLC)["Pl:time3","Ll:time2:DL"]+2*t3*t3*vcov(CLC)["Pl:time3","Pl:time3:DL"]-2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t3*t2*vcov(CLC)["Ll:time3","Pl:time2:DL"]+2*t3*t2*vcov(CLC)["Ll:time3","Ll:time2:DL"]-2*t3*t3*vcov(CLC)["Ll:time3","Pl:time3:DL"]+2*t3*t3*vcov(CLC)["Ll:time3","Ll:time3:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]+
      2*t4*t2*vcov(CLC)["Pl:time4","Pl:time2:DL"]-2*t4*t2*vcov(CLC)["Pl:time4","Ll:time2:DL"]+2*t4*t3*vcov(CLC)["Pl:time4","Pl:time3:DL"]-2*t4*t3*vcov(CLC)["Pl:time4","Ll:time3:DL"]+
      2*t4*t4*vcov(CLC)["Pl:time4","Pl:time4:DL"]-2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t4*t2*vcov(CLC)["Ll:time4","Pl:time2:DL"]+2*t4*t2*vcov(CLC)["Ll:time4","Ll:time2:DL"]-2*t4*t3*vcov(CLC)["Ll:time4","Pl:time3:DL"]+2*t4*t3*vcov(CLC)["Ll:time4","Ll:time3:DL"]-
      2*t4*t4*vcov(CLC)["Ll:time4","Pl:time4:DL"]+2*t4*t4*vcov(CLC)["Ll:time4","Ll:time4:DL"]-2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]+
      2*t5*t2*vcov(CLC)["Pl:time5","Pl:time2:DL"]-2*t5*t2*vcov(CLC)["Pl:time5","Ll:time2:DL"]+2*t5*t3*vcov(CLC)["Pl:time5","Pl:time3:DL"]-2*t5*t3*vcov(CLC)["Pl:time5","Ll:time3:DL"]+
      2*t5*t4*vcov(CLC)["Pl:time5","Pl:time4:DL"]-2*t5*t4*vcov(CLC)["Pl:time5","Ll:time4:DL"]+2*t5*t5*vcov(CLC)["Pl:time5","Pl:time5:DL"]-2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t5*t2*vcov(CLC)["Ll:time5","Pl:time2:DL"]+2*t5*t2*vcov(CLC)["Ll:time5","Ll:time2:DL"]-2*t5*t3*vcov(CLC)["Ll:time5","Pl:time3:DL"]+2*t5*t3*vcov(CLC)["Ll:time5","Ll:time3:DL"]-
      2*t5*t4*vcov(CLC)["Ll:time5","Pl:time4:DL"]+2*t5*t4*vcov(CLC)["Ll:time5","Ll:time4:DL"]-2*t5*t5*vcov(CLC)["Ll:time5","Pl:time5:DL"]+2*t5*t5*vcov(CLC)["Ll:time5","Ll:time5:DL"]-2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]+
      2*t6*t2*vcov(CLC)["Pl:time6","Pl:time2:DL"]-2*t6*t2*vcov(CLC)["Pl:time6","Ll:time2:DL"]+2*t6*t3*vcov(CLC)["Pl:time6","Pl:time3:DL"]-2*t6*t3*vcov(CLC)["Pl:time6","Ll:time3:DL"]+
      2*t6*t4*vcov(CLC)["Pl:time6","Pl:time4:DL"]-2*t6*t4*vcov(CLC)["Pl:time6","Ll:time4:DL"]+2*t6*t5*vcov(CLC)["Pl:time6","Pl:time5:DL"]-2*t6*t5*vcov(CLC)["Pl:time6","Ll:time5:DL"]+2*t6*t6*vcov(CLC)["Pl:time6","Pl:time6:DL"]-2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6:DL"]-
      2*t6*t2*vcov(CLC)["Ll:time6","Pl:time2:DL"]+2*t6*t2*vcov(CLC)["Ll:time6","Ll:time2:DL"]-2*t6*t3*vcov(CLC)["Ll:time6","Pl:time3:DL"]+2*t6*t3*vcov(CLC)["Ll:time6","Ll:time3:DL"]-
      2*t6*t4*vcov(CLC)["Ll:time6","Pl:time4:DL"]+2*t6*t4*vcov(CLC)["Ll:time6","Ll:time4:DL"]-2*t6*t5*vcov(CLC)["Ll:time6","Pl:time5:DL"]+2*t6*t5*vcov(CLC)["Ll:time6","Ll:time5:DL"]-2*t6*t6*vcov(CLC)["Ll:time6","Pl:time6:DL"]+2*t6*t6*vcov(CLC)["Ll:time6","Ll:time6:DL"]-
      2*t2*t2*vcov(CLC)["Pl:time2:DL","Ll:time2:DL"]+2*t2*t3*vcov(CLC)["Pl:time2:DL","Pl:time3:DL"]-2*t2*t3*vcov(CLC)["Pl:time2:DL","Ll:time3:DL"]+
      2*t2*t4*vcov(CLC)["Pl:time2:DL","Pl:time4:DL"]-2*t2*t4*vcov(CLC)["Pl:time2:DL","Ll:time4:DL"]+2*t2*t5*vcov(CLC)["Pl:time2:DL","Pl:time5:DL"]-2*t2*t5*vcov(CLC)["Pl:time2:DL","Ll:time5:DL"]+2*t2*t6*vcov(CLC)["Pl:time2:DL","Pl:time6:DL"]-2*t2*t6*vcov(CLC)["Pl:time2:DL","Ll:time6:DL"]-
      2*t2*t3*vcov(CLC)["Ll:time2:DL","Pl:time3:DL"]+2*t2*t3*vcov(CLC)["Ll:time2:DL","Ll:time3:DL"]-
      2*t2*t4*vcov(CLC)["Ll:time2:DL","Pl:time4:DL"]+2*t2*t4*vcov(CLC)["Ll:time2:DL","Ll:time4:DL"]-2*t2*t5*vcov(CLC)["Ll:time2:DL","Pl:time5:DL"]+2*t2*t5*vcov(CLC)["Ll:time2:DL","Ll:time5:DL"]-2*t2*t6*vcov(CLC)["Ll:time2:DL","Pl:time6:DL"]+2*t2*t6*vcov(CLC)["Ll:time2:DL","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3:DL","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3:DL","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3:DL","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3:DL","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3:DL","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3:DL","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3:DL","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3:DL","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3:DL","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3:DL","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3:DL","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3:DL","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3:DL","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4:DL","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4:DL","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4:DL","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4:DL","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4:DL","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4:DL","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4:DL","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4:DL","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4:DL","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5:DL","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5:DL","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5:DL","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5:DL","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5:DL","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6:DL","Ll:time6:DL"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
    #right:
    CI.PlLl[t,5]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6-
      (CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6)#fitted values
    CI.PlLl[t,6]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]-
      2*1*1*vcov(CLC)["Pl_ISL","Ll_ISL"]+2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]
    CI.PlLl[t,7]=CI.PlLl[t,5]-qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI lower
    CI.PlLl[t,8]=CI.PlLl[t,5]+qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}#wo Pl:time1:DL,Ll:time1:DL
CI.PlLllses10CGfun2=function(CLC,t0,pe){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=8)#Pl:
  for (t in 1:59) {
    t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    #left:
    CI.PlLl[t,1]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6+
      CLC$coefficients["Pl:time3:DL"]*t3+CLC$coefficients["Pl:time4:DL"]*t4+CLC$coefficients["Pl:time5:DL"]*t5+CLC$coefficients["Pl:time6:DL"]*t6-
      (CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6+
         CLC$coefficients["Ll:time3:DL"]*t3+CLC$coefficients["Ll:time4:DL"]*t4+CLC$coefficients["Ll:time5:DL"]*t5+CLC$coefficients["Ll:time6:DL"]*t6)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      t3^2*vcov(CLC)["Pl:time3:DL","Pl:time3:DL"]+t4^2*vcov(CLC)["Pl:time4:DL","Pl:time4:DL"]+t5^2*vcov(CLC)["Pl:time5:DL","Pl:time5:DL"]+t6^2*vcov(CLC)["Pl:time6:DL","Pl:time6:DL"]+
      1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      t3^2*vcov(CLC)["Ll:time3:DL","Ll:time3:DL"]+t4^2*vcov(CLC)["Ll:time4:DL","Ll:time4:DL"]+t5^2*vcov(CLC)["Ll:time5:DL","Ll:time5:DL"]+t6^2*vcov(CLC)["Ll:time6:DL","Ll:time6:DL"]-
      2*1*1*vcov(CLC)["Pl_ISL","Ll_ISL"]+2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6"]+
      2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3:DL"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3:DL"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4:DL"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4:DL"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5:DL"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5:DL"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6:DL"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6:DL"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]-
      2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3:DL"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3:DL"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4:DL"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4:DL"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5:DL"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5:DL"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6:DL"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6:DL"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]+
      2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3:DL"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3:DL"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4:DL"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4:DL"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5:DL"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5:DL"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6:DL"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6:DL"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3:DL"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3:DL"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4:DL"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4:DL"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5:DL"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5:DL"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6:DL"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]+
      2*t3*t3*vcov(CLC)["Pl:time3","Pl:time3:DL"]-2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t3*t3*vcov(CLC)["Ll:time3","Pl:time3:DL"]+2*t3*t3*vcov(CLC)["Ll:time3","Ll:time3:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]+
      2*t4*t3*vcov(CLC)["Pl:time4","Pl:time3:DL"]-2*t4*t3*vcov(CLC)["Pl:time4","Ll:time3:DL"]+
      2*t4*t4*vcov(CLC)["Pl:time4","Pl:time4:DL"]-2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t4*t3*vcov(CLC)["Ll:time4","Pl:time3:DL"]+2*t4*t3*vcov(CLC)["Ll:time4","Ll:time3:DL"]-
      2*t4*t4*vcov(CLC)["Ll:time4","Pl:time4:DL"]+2*t4*t4*vcov(CLC)["Ll:time4","Ll:time4:DL"]-2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]+
      2*t5*t3*vcov(CLC)["Pl:time5","Pl:time3:DL"]-2*t5*t3*vcov(CLC)["Pl:time5","Ll:time3:DL"]+
      2*t5*t4*vcov(CLC)["Pl:time5","Pl:time4:DL"]-2*t5*t4*vcov(CLC)["Pl:time5","Ll:time4:DL"]+2*t5*t5*vcov(CLC)["Pl:time5","Pl:time5:DL"]-2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t5*t3*vcov(CLC)["Ll:time5","Pl:time3:DL"]+2*t5*t3*vcov(CLC)["Ll:time5","Ll:time3:DL"]-
      2*t5*t4*vcov(CLC)["Ll:time5","Pl:time4:DL"]+2*t5*t4*vcov(CLC)["Ll:time5","Ll:time4:DL"]-2*t5*t5*vcov(CLC)["Ll:time5","Pl:time5:DL"]+2*t5*t5*vcov(CLC)["Ll:time5","Ll:time5:DL"]-2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]+
      2*t6*t3*vcov(CLC)["Pl:time6","Pl:time3:DL"]-2*t6*t3*vcov(CLC)["Pl:time6","Ll:time3:DL"]+
      2*t6*t4*vcov(CLC)["Pl:time6","Pl:time4:DL"]-2*t6*t4*vcov(CLC)["Pl:time6","Ll:time4:DL"]+2*t6*t5*vcov(CLC)["Pl:time6","Pl:time5:DL"]-2*t6*t5*vcov(CLC)["Pl:time6","Ll:time5:DL"]+2*t6*t6*vcov(CLC)["Pl:time6","Pl:time6:DL"]-2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6:DL"]-
      2*t6*t3*vcov(CLC)["Ll:time6","Pl:time3:DL"]+2*t6*t3*vcov(CLC)["Ll:time6","Ll:time3:DL"]-
      2*t6*t4*vcov(CLC)["Ll:time6","Pl:time4:DL"]+2*t6*t4*vcov(CLC)["Ll:time6","Ll:time4:DL"]-2*t6*t5*vcov(CLC)["Ll:time6","Pl:time5:DL"]+2*t6*t5*vcov(CLC)["Ll:time6","Ll:time5:DL"]-2*t6*t6*vcov(CLC)["Ll:time6","Pl:time6:DL"]+2*t6*t6*vcov(CLC)["Ll:time6","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3:DL","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3:DL","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3:DL","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3:DL","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3:DL","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3:DL","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3:DL","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3:DL","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3:DL","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3:DL","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3:DL","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3:DL","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3:DL","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4:DL","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4:DL","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4:DL","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4:DL","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4:DL","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4:DL","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4:DL","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4:DL","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4:DL","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5:DL","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5:DL","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5:DL","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5:DL","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5:DL","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6:DL","Ll:time6:DL"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
    #right:
    CI.PlLl[t,5]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6-
      (CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6)#fitted values
    CI.PlLl[t,6]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]-
      2*1*1*vcov(CLC)["Pl_ISL","Ll_ISL"]+2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]
    CI.PlLl[t,7]=CI.PlLl[t,5]-qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI lower
    CI.PlLl[t,8]=CI.PlLl[t,5]+qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}#wo Pl:time1:DL,Ll:time1:DL,Pl:time2:DL,Ll:time2:DL

#Z-test functions:
ZtestfunT12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTD12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2,":DL")]-CLC$coefficients[paste0("Ll:time",2,":DL")]#mean
  v=vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Pl:time",2,":DL")]+vcov(CLC)[paste0("Ll:time",2,":DL"),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Ll:time",2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunD12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]+
    CLC$coefficients[paste0("Pl:time",2,":DL")]-CLC$coefficients[paste0("Ll:time",2,":DL")]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]+
    vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Pl:time",2,":DL")]+vcov(CLC)[paste0("Ll:time",2,":DL"),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]+2*vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",2),paste0("Pl:time",2,":DL")]+2*vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Ll:time",2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunT=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTD=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1,":DL")]-CLC$coefficients[paste0("Ll:time",per1,":DL")]-
        CLC$coefficients[paste0("Pl:time",per2,":DL")]+CLC$coefficients[paste0("Ll:time",per2,":DL")])#mean
  v=vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per1,":DL")]+vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per1,":DL")]+
    vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Pl:time",per2,":DL")]+vcov(CLC)[paste0("Ll:time",per2,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2,":DL")]-2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Ll:time",per2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunD=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]+
        CLC$coefficients[paste0("Pl:time",per1,":DL")]-CLC$coefficients[paste0("Ll:time",per1,":DL")]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)]-
        CLC$coefficients[paste0("Pl:time",per2,":DL")]+CLC$coefficients[paste0("Ll:time",per2,":DL")])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per1,":DL")]+vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per1,":DL")]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]+
    vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Pl:time",per2,":DL")]+vcov(CLC)[paste0("Ll:time",per2,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]+2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per1,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1,":DL")]+2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2)]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2)]-
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per2),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Ll:time",per2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}


##Table D10##
##starting value for thresholds -4
#time variables:
t0=-4#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regression:
CLC_lses10_DEU_m4=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                        Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                        +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                      +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses10_DEU_m4$coefficients)[names(CLC_lses10_DEU_m4$coefficients)=="time2:Ll"]="Ll:time2"
names(CLC_lses10_DEU_m4$coefficients)[names(CLC_lses10_DEU_m4$coefficients)=="time2:Ll:DL"]="Ll:time2:DL"

#hypothesis testing:
stargazer(CLC_lses10_DEU_m4,no.space = T,single.row = T)
#drop others (DL==0):
ZtestfunT12(CLC_lses10_DEU_m4)
ZtestfunT(CLC_lses10_DEU_m4,2,3)
ZtestfunT(CLC_lses10_DEU_m4,3,4)
ZtestfunT(CLC_lses10_DEU_m4,4,5)
ZtestfunT(CLC_lses10_DEU_m4,5,6)
#drop Germany (DL==1):
# ZtestfunD12(CLC_lses10_DEU_m4)
# ZtestfunD(CLC_lses10_DEU_m4,2,3)
ZtestfunD(CLC_lses10_DEU_m4,3,4)
ZtestfunD(CLC_lses10_DEU_m4,4,5)
ZtestfunD(CLC_lses10_DEU_m4,5,6)
#difference in drop between Germany and others (DL==1 vs DL==0):
# ZtestfunTD12(CLC_lses10_DEU_m4)
# ZtestfunTD(CLC_lses10_DEU_m4,2,3)
ZtestfunTD(CLC_lses10_DEU_m4,3,4)
ZtestfunTD(CLC_lses10_DEU_m4,4,5)
ZtestfunTD(CLC_lses10_DEU_m4,5,6)

#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_DEU_m4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.2001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.2001)+0.2001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun2(CLC_lses10_DEU_m4,-4,1)) +#(high)
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun2(CLC_lses10_DEU_m4,-4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun2(CLC_lses10_DEU_m4,-4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun2(CLC_lses10_DEU_m4,-4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun2(CLC_lses10_DEU_m4,-4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun2(CLC_lses10_DEU_m4,-4,6)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun2(CLC_lses10_DEU_m4,-4,1)) +#(low)
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun2(CLC_lses10_DEU_m4,-4,2)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun2(CLC_lses10_DEU_m4,-4,3)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun2(CLC_lses10_DEU_m4,-4,4)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun2(CLC_lses10_DEU_m4,-4,5)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun2(CLC_lses10_DEU_m4,-4,6)) )
# dev.off()


##starting value for thresholds -2
#time variables:
t0=-2#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regression:
CLC_lses10_DEU_m2=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                           Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                           +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                         +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses10_DEU_m2$coefficients)[names(CLC_lses10_DEU_m2$coefficients)=="time2:Ll"]="Ll:time2"
names(CLC_lses10_DEU_m2$coefficients)[names(CLC_lses10_DEU_m2$coefficients)=="time2:Ll:DL"]="Ll:time2:DL"

#hypothesis testing:
stargazer(CLC_lses10_DEU_m2,no.space = T,single.row = T)
#drop others (DL==0):
ZtestfunT12(CLC_lses10_DEU_m2)
ZtestfunT(CLC_lses10_DEU_m2,2,3)
ZtestfunT(CLC_lses10_DEU_m2,3,4)
ZtestfunT(CLC_lses10_DEU_m2,4,5)
ZtestfunT(CLC_lses10_DEU_m2,5,6)
#drop Germany (DL==1):
ZtestfunD12(CLC_lses10_DEU_m2)
ZtestfunD(CLC_lses10_DEU_m2,2,3)
ZtestfunD(CLC_lses10_DEU_m2,3,4)
ZtestfunD(CLC_lses10_DEU_m2,4,5)
ZtestfunD(CLC_lses10_DEU_m2,5,6)
#difference in drop between Germany and others (DL==1 vs DL==0):
ZtestfunTD12(CLC_lses10_DEU_m2)
ZtestfunTD(CLC_lses10_DEU_m2,2,3)
ZtestfunTD(CLC_lses10_DEU_m2,3,4)
ZtestfunTD(CLC_lses10_DEU_m2,4,5)
ZtestfunTD(CLC_lses10_DEU_m2,5,6)

#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_DEU_m2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.2001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.2001)+0.2001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_m2,-2,1)) +#(high)
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_m2,-2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_m2,-2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_m2,-2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_m2,-2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_m2,-2,6)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_m2,-2,1)) +#(low)
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_m2,-2,2)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_m2,-2,3)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_m2,-2,4)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_m2,-2,5)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_m2,-2,6)) )
# dev.off()


##starting value for thresholds 2
#time variables:
t0=2#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regression:
CLC_lses10_DEU_2=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                           Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                           +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                         +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses10_DEU_2$coefficients)[names(CLC_lses10_DEU_2$coefficients)=="time2:Ll"]="Ll:time2"
names(CLC_lses10_DEU_2$coefficients)[names(CLC_lses10_DEU_2$coefficients)=="time2:Ll:DL"]="Ll:time2:DL"

#hypothesis testing:
stargazer(CLC_lses10_DEU_2,no.space = T,single.row = T)
#drop others (DL==0):
ZtestfunT12(CLC_lses10_DEU_2)
ZtestfunT(CLC_lses10_DEU_2,2,3)
ZtestfunT(CLC_lses10_DEU_2,3,4)
ZtestfunT(CLC_lses10_DEU_2,4,5)
ZtestfunT(CLC_lses10_DEU_2,5,6)
#drop Germany (DL==1):
ZtestfunD12(CLC_lses10_DEU_2)
ZtestfunD(CLC_lses10_DEU_2,2,3)
ZtestfunD(CLC_lses10_DEU_2,3,4)
ZtestfunD(CLC_lses10_DEU_2,4,5)
ZtestfunD(CLC_lses10_DEU_2,5,6)
#difference in drop between Germany and others (DL==1 vs DL==0):
ZtestfunTD12(CLC_lses10_DEU_2)
ZtestfunTD(CLC_lses10_DEU_2,2,3)
ZtestfunTD(CLC_lses10_DEU_2,3,4)
ZtestfunTD(CLC_lses10_DEU_2,4,5)
ZtestfunTD(CLC_lses10_DEU_2,5,6)

#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_DEU_2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.2001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.2001)+0.2001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_2,2,1)) +#(high)
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_2,2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_2,2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_2,2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_2,2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_2,2,6)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_2,2,1)) +#(low)
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_2,2,2)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_2,2,3)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_2,2,4)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_2,2,5)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_2,2,6)) )
# dev.off()


##starting value for thresholds 4
#time variables:
t0=4#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regression:
CLC_lses10_DEU_4=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                          Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                          +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                        +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses10_DEU_4$coefficients)[names(CLC_lses10_DEU_4$coefficients)=="time2:Ll"]="Ll:time2"
names(CLC_lses10_DEU_4$coefficients)[names(CLC_lses10_DEU_4$coefficients)=="time2:Ll:DL"]="Ll:time2:DL"

#hypothesis testing:
stargazer(CLC_lses10_DEU_4,no.space = T,single.row = T)
#drop others (DL==0):
ZtestfunT12(CLC_lses10_DEU_4)
ZtestfunT(CLC_lses10_DEU_4,2,3)
ZtestfunT(CLC_lses10_DEU_4,3,4)
ZtestfunT(CLC_lses10_DEU_4,4,5)
ZtestfunT(CLC_lses10_DEU_4,5,6)
#drop Germany (DL==1):
ZtestfunD12(CLC_lses10_DEU_4)
ZtestfunD(CLC_lses10_DEU_4,2,3)
ZtestfunD(CLC_lses10_DEU_4,3,4)
ZtestfunD(CLC_lses10_DEU_4,4,5)
ZtestfunD(CLC_lses10_DEU_4,5,6)
#difference in drop between Germany and others (DL==1 vs DL==0):
ZtestfunTD12(CLC_lses10_DEU_4)
ZtestfunTD(CLC_lses10_DEU_4,2,3)
ZtestfunTD(CLC_lses10_DEU_4,3,4)
ZtestfunTD(CLC_lses10_DEU_4,4,5)
ZtestfunTD(CLC_lses10_DEU_4,5,6)

#plots (median cty):
# tikz(paste0("plot_","CLC_lses10_DEU_4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.2001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.2001)+0.2001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_4,4,1)) +#(high)
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_4,4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_4,4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_4,4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_4,4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses10_DEU_4,4,6)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_4,4,1)) +#(low)
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_4,4,2)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_4,4,3)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_4,4,4)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_4,4,5)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses10_DEU_4,4,6)) )
# dev.off()


##Table I8##
##table full results:
cm4=CLC_lses10_DEU_m4#shorten name for stargazer:
cm2=CLC_lses10_DEU_m2
c0=CLC_lses10_DEU
c2=CLC_lses10_DEU_2
c4=CLC_lses10_DEU_4
stargazer(cm4,cm2,c0,c2,c4)#table
rm(cm4,cm2,c0,c2,c4)


#THEORY CMP CATEGORIES:----

#prepare data:
dc=read.xlsx("MPDataset_MPDS2020b.xlsx")#read CMP project data
dc$pertot=rowSums(dc[,26:81],na.rm=T)#total per coded
dc$perrile=rowSums(dc[,names(dc) %in% c("per104","per201","per203","per305","per401","per402","per407","per414","per505",
                                        "per601","per603","per605","per606","per103","per105","per106","per107","per403",
                                        "per404","per406","per412","per413","per504","per506","per701","per202")],na.rm=T)#total abs per rile
dc$sharerile=dc$perrile/dc$pertot#share per rile/tot per
dc$Ey=as.numeric(substr(as.numeric(dc$date),1,4))#code year
dc=dc[dc$Ey>1960,]#keep only years in de dataset
dc=dc[dc$countryname %in% c("Australia","Austria","Canada","Denmark","Finland","Germany","United Kingdom","Greece","Iceland","Ireland"
                            ,"Israel","Italy","Netherlands","New Zealand","Norway","Portugal","Spain","Sweden"),]#keep only countries in de
colnames(dc)[colnames(dc)=="countryname"]="Ec"
temp=c("AUS","AUT","CAN","DNK","FIN","DEU","GBR","GRC","ISL","IRL"#assign same names Ec in de
       ,"ISR","ITA","NLD","NZL","NOR","PRT","ESP","SWE")#new cty names
names(temp)=c("Australia","Austria","Canada","Denmark","Finland","Germany","United Kingdom","Greece","Iceland","Ireland"
              ,"Israel","Italy","Netherlands","New Zealand","Norway","Portugal","Spain","Sweden")#old cty names
dc$Ec[dc$Ec %in% names(temp)]=temp[dc$Ec[dc$Ec %in% names(temp)]]#assign new cty names

# dc=dc %>% group_by(Ec,Ey) %>% mutate(sharerile_cy=weightedMean(sharerile,w=pervote,na.rm=TRUE))#avg share rile by Ec,Ey
dc=dc %>% group_by(Ec,Ey) %>% mutate(sharerile_cy=mean(sharerile,na.rm=TRUE))#avg share rile by Ec,Ey
dc=dc %>% group_by(Ec,Ey) %>% filter(row_number()==1)#keep only 1 Ec for Ey
dc$i=c(1:nrow(dc))#create index for robust lm estimation
dc$time=dc$Ey-1960#time
# dc=merge(ds,dc,by=c("Ec","Ey"),all.x=T,all.y=F)#to limit to ds (and de) observations/elections
dc$sharerile_cy=dc$sharerile_cy*100#use % instead than share

#CI function:
CI.LM_dc=function(LM,t0,pe){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    # t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.PlLl[t,1]=LM$coefficients[paste0("(Intercept)")]+LM$coefficients["time2"]*t2+LM$coefficients["time3"]*t3+LM$coefficients["time4"]*t4+LM$coefficients["time5"]*t5+LM$coefficients["time6"]*t6#fitted values
    CI.PlLl[t,2]=1*vcov(LM)[paste0("(Intercept)"),paste0("(Intercept)")]+t2^2*vcov(LM)["time2","time2"]+t3^2*vcov(LM)["time3","time3"]+t4^2*vcov(LM)["time4","time4"]+t5^2*vcov(LM)["time5","time5"]+t6^2*vcov(LM)["time6","time6"]+
      2*1*t2*vcov(LM)[paste0("(Intercept)"),"time2"]+2*1*t3*vcov(LM)[paste0("(Intercept)"),"time3"]+2*1*t4*vcov(LM)[paste0("(Intercept)"),"time4"]+2*1*t5*vcov(LM)[paste0("(Intercept)"),"time5"]+2*1*t6*vcov(LM)[paste0("(Intercept)"),"time6"]+
      2*t2*t3*vcov(LM)["time2","time3"]+2*t2*t4*vcov(LM)["time2","time4"]+2*t2*t5*vcov(LM)["time2","time5"]+2*t2*t6*vcov(LM)["time2","time6"]+
      2*t3*t4*vcov(LM)["time3","time4"]+2*t3*t5*vcov(LM)["time3","time5"]+2*t3*t6*vcov(LM)["time3","time6"]+
      2*t4*t5*vcov(LM)["time4","time5"]+2*t4*t6*vcov(LM)["time4","time6"]+
      2*t5*t6*vcov(LM)["time5","time6"]#Variance
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}#w/constant
CI.LM_dc=function(LM,t0,pe){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    # t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.PlLl[t,1]=LM$coefficients[paste0("factor(Ec)ISL")]+LM$coefficients["time2"]*t2+LM$coefficients["time3"]*t3+LM$coefficients["time4"]*t4+LM$coefficients["time5"]*t5+LM$coefficients["time6"]*t6#fitted values
    CI.PlLl[t,2]=1*vcov(LM)[paste0("factor(Ec)ISL"),paste0("factor(Ec)ISL")]+t2^2*vcov(LM)["time2","time2"]+t3^2*vcov(LM)["time3","time3"]+t4^2*vcov(LM)["time4","time4"]+t5^2*vcov(LM)["time5","time5"]+t6^2*vcov(LM)["time6","time6"]+
      2*1*t2*vcov(LM)[paste0("factor(Ec)ISL"),"time2"]+2*1*t3*vcov(LM)[paste0("factor(Ec)ISL"),"time3"]+2*1*t4*vcov(LM)[paste0("factor(Ec)ISL"),"time4"]+2*1*t5*vcov(LM)[paste0("factor(Ec)ISL"),"time5"]+2*1*t6*vcov(LM)[paste0("factor(Ec)ISL"),"time6"]+
      2*t2*t3*vcov(LM)["time2","time3"]+2*t2*t4*vcov(LM)["time2","time4"]+2*t2*t5*vcov(LM)["time2","time5"]+2*t2*t6*vcov(LM)["time2","time6"]+
      2*t3*t4*vcov(LM)["time3","time4"]+2*t3*t5*vcov(LM)["time3","time5"]+2*t3*t6*vcov(LM)["time3","time6"]+
      2*t4*t5*vcov(LM)["time4","time5"]+2*t4*t6*vcov(LM)["time4","time6"]+
      2*t5*t6*vcov(LM)["time5","time6"]#Variance
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}#w/cty FE

#Z-test functions:
Ztestfun=function(CLC,per1,per2){
  m=CLC$coefficients[paste0("time",per2)]-CLC$coefficients[paste0("time",per1)]#mean
  v=vcov(CLC)[paste0("time",per1),paste0("time",per1)]+vcov(CLC)[paste0("time",per2),paste0("time",per2)]-
    vcov(CLC)[paste0("time",per1),paste0("time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}


##rile scale:
#create dataset density dydc_10:
dydc_10 = dc %>% group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dydc_10$Eyn=dydc_10$Eyn/nrow(dydc_10)#density by Ey
dydc_10=dydc_10 %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey
# dydc_10$Eyn=dydc_10$Eyn*50#density by Ey scaled up for graphs
# dydc_10$Eyn=dydc_10$Eyn+45#density by Ey shifted upward for graphs
#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
L_dc_10=lm(sharerile_cy~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc)
LM_dc_10=lm_robust(sharerile_cy~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i)


##Table 8## Left - Right
#plots (median cty):
# tikz(paste0("plot_","LM_dc_10.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Left$-$Right") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(45.01,60)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(60-45.01)+45.01))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_10) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10,0,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10,0,6)) )
# dev.off()
#table (hypothesis testing):
LM_dc_10$r.squared
LM_dc_10$nobs
c(LM_dc_10$coefficients["time2"],LM_dc_10$std.error["time2"],LM_dc_10$p.value["time2"])#t2-t1=time2
Ztestfun(LM_dc_10,2,3)
Ztestfun(LM_dc_10,3,4)
Ztestfun(LM_dc_10,4,5)
Ztestfun(LM_dc_10,5,6)


##biggest increases and decreases in rile:
dctemp=dc[,names(dc) %in% c(paste0("per",101:706),"Ey")]
dctemp$y20[dctemp$Ey>=1960&dctemp$Ey<1980]=1
dctemp$y20[dctemp$Ey>=1980&dctemp$Ey<2000]=2
dctemp$y20[dctemp$Ey>=2000]=3
dctemp[nrow(dctemp)+1,]=t(colMeans(dctemp[dctemp$y20==1,],na.rm=T))
dctemp[nrow(dctemp)+1,]=t(colMeans(dctemp[dctemp$y20==2,],na.rm=T))
dctemp[nrow(dctemp)+1,]=t(colMeans(dctemp[dctemp$y20==3,],na.rm=T))
dctemp=dctemp[303:305,]
dctemp[nrow(dctemp)+1,]=dctemp[3,]-dctemp[1,]
dctemp=as.matrix(dctemp)
sort(dctemp[4,],decreasing=T)#biggest increases per501; per503
# sort(dctemp[4,])#biggest decreases per404; per202
rm(dctemp)

#create dataset density dydc_per:
dydc_per = dc %>% group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dydc_per$Eyn=dydc_per$Eyn/nrow(dydc_per)#density by Ey
dydc_per=dydc_per %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey
# dydc_per$Eyn=dydc_per$Eyn*20#density by Ey scaled down for graphs
# dydc_per$Eyn=dydc_per$Eyn+0#density by Ey shifted upward for graphs


##per501 Environmental Protection:
# General policies in favour of protecting the environment, fighting climate change,
#  and other “green” policies. For instance:
# • General preservation of natural resources; 
# • Preservation of countryside, forests, etc.; 
# • Protection of national parks;
# • Animal rights.
#regression:
L_dc_10_per501=lm(per501~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,weights=pervote)
LM_dc_10_per501=lm_robust(per501~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i,weights=pervote)


##Table 8## Environmental Protection
#plots (median cty):
# tikz(paste0("plot_","LM_dc_10_per501.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Environmental Protection") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(0.0001,15)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(15-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_per) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_per501,0,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_per501,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_per501,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_per501,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_per501,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_per501,0,6)) )
# dev.off()
#table (hypothesis testing):
LM_dc_10_per501$r.squared
LM_dc_10_per501$nobs
c(LM_dc_10_per501$coefficients["time2"],LM_dc_10_per501$std.error["time2"],LM_dc_10_per501$p.value["time2"])#t2-t1=time2
Ztestfun(LM_dc_10_per501,2,3)
Ztestfun(LM_dc_10_per501,3,4)
Ztestfun(LM_dc_10_per501,4,5)
Ztestfun(LM_dc_10_per501,5,6)


##per503 Equality: Positive:
# Concept of social justice and the need for fair treatment of all people. This may include:
# • Special protection for underprivileged social groups;
# • Removal of class barriers;
# • Need for fair distribution of resources;
# • The end of discrimination (e.g. racial or sexual discrimination).
#regression:
L_dc_10_per503=lm(per503~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,weights=pervote)
LM_dc_10_per503=lm_robust(per503~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i,weights=pervote)


##Table 8## Equality Positive
#plots (median cty):
# tikz(paste0("plot_","LM_dc_10_per503.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Equality, Positive") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(0.0001,15)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(15-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_per) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_per503,0,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_per503,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_per503,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_per503,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_per503,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_per503,0,6)) )
# dev.off()
LM_dc_10_per503$r.squared
LM_dc_10_per503$nobs
c(LM_dc_10_per503$coefficients["time2"],LM_dc_10_per503$std.error["time2"],LM_dc_10_per503$p.value["time2"])#t2-t1=time2
Ztestfun(LM_dc_10_per503,2,3)
Ztestfun(LM_dc_10_per503,3,4)
Ztestfun(LM_dc_10_per503,4,5)
Ztestfun(LM_dc_10_per503,5,6)


##THEORY CMP CATEGORIES - RILE - ALTERNATIVE THRESHOLDS:----

##Table D11##
##starting value for thresholds -4
#time variables:
t0=-4#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
L_dc_10_m4=lm(sharerile_cy~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc)
LM_dc_10_m4=lm_robust(sharerile_cy~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i)
#plots (median cty):
# tikz(paste0("plot_","LM_dc_10_m4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Left$-$Right") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(45.01,60)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(60-45.01)+45.01))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_10) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_m4,-4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_m4,-4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_m4,-4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_m4,-4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_m4,-4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_m4,-4,6)) )
# dev.off()
#table (hypothesis testing):
LM_dc_10_m4$r.squared
LM_dc_10_m4$nobs
c(LM_dc_10_m4$coefficients["time2"],LM_dc_10_m4$std.error["time2"],LM_dc_10_m4$p.value["time2"])#t2-t1=time2
Ztestfun(LM_dc_10_m4,2,3)
Ztestfun(LM_dc_10_m4,3,4)
Ztestfun(LM_dc_10_m4,4,5)
Ztestfun(LM_dc_10_m4,5,6)


##starting value for thresholds -2
#time variables:
t0=-2#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
L_dc_10_m2=lm(sharerile_cy~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc)
LM_dc_10_m2=lm_robust(sharerile_cy~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i)
#plots (median cty):
# tikz(paste0("plot_","LM_dc_10_m2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Left$-$Right") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(45.01,60)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(60-45.01)+45.01))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_10) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_m2,-2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_m2,-2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_m2,-2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_m2,-2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_m2,-2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_m2,-2,6)) )
# dev.off()
#table (hypothesis testing):
LM_dc_10_m2$r.squared
LM_dc_10_m2$nobs
c(LM_dc_10_m2$coefficients["time2"],LM_dc_10_m2$std.error["time2"],LM_dc_10_m2$p.value["time2"])#t2-t1=time2
Ztestfun(LM_dc_10_m2,2,3)
Ztestfun(LM_dc_10_m2,3,4)
Ztestfun(LM_dc_10_m2,4,5)
Ztestfun(LM_dc_10_m2,5,6)


##starting value for thresholds 2
#time variables:
t0=2#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
L_dc_10_2=lm(sharerile_cy~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc)
LM_dc_10_2=lm_robust(sharerile_cy~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i)
#plots (median cty):
# tikz(paste0("plot_","LM_dc_10_2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Left$-$Right") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(45.01,60)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(60-45.01)+45.01))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_10) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_2,2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_2,2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_2,2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_2,2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_2,2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_2,2,6)) )
# dev.off()
# table (hypothesis testing):
LM_dc_10_2$r.squared
LM_dc_10_2$nobs
c(LM_dc_10_2$coefficients["time2"],LM_dc_10_2$std.error["time2"],LM_dc_10_2$p.value["time2"])#t2-t1=time2
Ztestfun(LM_dc_10_2,2,3)
Ztestfun(LM_dc_10_2,3,4)
Ztestfun(LM_dc_10_2,4,5)
Ztestfun(LM_dc_10_2,5,6)


##starting value for thresholds 4
#time variables:
t0=4#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
L_dc_10_4=lm(sharerile_cy~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc)
LM_dc_10_4=lm_robust(sharerile_cy~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i)
#plots (median cty):
# tikz(paste0("plot_","LM_dc_10_4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Left$-$Right") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(45.01,60)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(60-45.01)+45.01))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_10) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_4,4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_4,4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_4,4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_4,4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_4,4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LM_dc_10_4,4,6)) )
# dev.off()
# table (hypothesis testing):
LM_dc_10_4$r.squared
LM_dc_10_4$nobs
c(LM_dc_10_4$coefficients["time2"],LM_dc_10_4$std.error["time2"],LM_dc_10_4$p.value["time2"])#t2-t1=time2
Ztestfun(LM_dc_10_4,2,3)
Ztestfun(LM_dc_10_4,3,4)
Ztestfun(LM_dc_10_4,4,5)
Ztestfun(LM_dc_10_4,5,6)


##Table I9##
##table full results:
stargazer(L_dc_10_m4,L_dc_10_m2,L_dc_10,L_dc_10_2,L_dc_10_4,
          se = list(LM_dc_10_m4$std.error,LM_dc_10_m2$std.error,LM_dc_10$std.error,LM_dc_10_2$std.error,LM_dc_10_4$std.error))


##THEORY CMP CATEGORIES - ENVIRONMENTAL P. - ALTERNATIVE THRESHOLDS:----

##Table D12##
##starting value for thresholds -4
#time variables:
t0=-4#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
Lp5_m4=lm(per501~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,weights=pervote)
LMp5_m4=lm_robust(per501~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i,weights=pervote)
#plots (median cty):
# tikz(paste0("plot_","LMp5_m4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Environmental Protection") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(0.0001,15)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(15-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_per) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_m4,-4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_m4,-4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_m4,-4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_m4,-4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_m4,-4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_m4,-4,6)) )
# dev.off()
#table (hypothesis testing):
LMp5_m4$r.squared
LMp5_m4$nobs
c(LMp5_m4$coefficients["time2"],LMp5_m4$std.error["time2"],LMp5_m4$p.value["time2"])#t2-t1=time2
Ztestfun(LMp5_m4,2,3)
Ztestfun(LMp5_m4,3,4)
Ztestfun(LMp5_m4,4,5)
Ztestfun(LMp5_m4,5,6)


##starting value for thresholds -2
#time variables:
t0=-2#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
Lp5_m2=lm(per501~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,weights=pervote)
LMp5_m2=lm_robust(per501~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i,weights=pervote)
#plots (median cty):
# tikz(paste0("plot_","LMp5_m2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Environmental Protection") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(0.0001,15)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(15-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_per) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_m2,-2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_m2,-2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_m2,-2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_m2,-2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_m2,-2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_m2,-2,6)) )
# dev.off()
#table (hypothesis testing):
LMp5_m2$r.squared
LMp5_m2$nobs
c(LMp5_m2$coefficients["time2"],LMp5_m2$std.error["time2"],LMp5_m2$p.value["time2"])#t2-t1=time2
Ztestfun(LMp5_m2,2,3)
Ztestfun(LMp5_m2,3,4)
Ztestfun(LMp5_m2,4,5)
Ztestfun(LMp5_m2,5,6)


##starting value for thresholds 2
#time variables:
t0=2#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
Lp5_2=lm(per501~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,weights=pervote)
LMp5_2=lm_robust(per501~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i,weights=pervote)
#plots (median cty):
# tikz(paste0("plot_","LMp5_2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Environmental Protection") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(0.0001,15)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(15-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_per) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_2,2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_2,2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_2,2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_2,2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_2,2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_2,2,6)) )
# dev.off()
#table (hypothesis testing):
LMp5_2$r.squared
LMp5_2$nobs
c(LMp5_2$coefficients["time2"],LMp5_2$std.error["time2"],LMp5_2$p.value["time2"])#t2-t1=time2
Ztestfun(LMp5_2,2,3)
Ztestfun(LMp5_2,3,4)
Ztestfun(LMp5_2,4,5)
Ztestfun(LMp5_2,5,6)


##starting value for thresholds 4
#time variables:
t0=4#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
Lp5_4=lm(per501~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,weights=pervote)
LMp5_4=lm_robust(per501~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i,weights=pervote)
#plots (median cty):
# tikz(paste0("plot_","LMp5_4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Environmental Protection") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(0.0001,15)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(15-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_per) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_4,4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_4,4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_4,4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_4,4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_4,4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp5_4,4,6)) )
# dev.off()
#table (hypothesis testing):
LMp5_4$r.squared
LMp5_4$nobs
c(LMp5_4$coefficients["time2"],LMp5_4$std.error["time2"],LMp5_4$p.value["time2"])#t2-t1=time2
Ztestfun(LMp5_4,2,3)
Ztestfun(LMp5_4,3,4)
Ztestfun(LMp5_4,4,5)
Ztestfun(LMp5_4,5,6)


##Table I10##
##table full results:
stargazer(Lp5_m4,Lp5_m2,L_dc_10_per501,Lp5_2,Lp5_4,
          se = list(LMp5_m4$std.error,LMp5_m2$std.error,LM_dc_10_per501$std.error,LMp5_2$std.error,LMp5_4$std.error))


##THEORY CMP CATEGORIES - EQUALITY P. - ALTERNATIVE THRESHOLDS:----

##Table D13##
##starting value for thresholds -4
#time variables:
t0=-4#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
Lp3_m4=lm(per503~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,weights=pervote)
LMp3_m4=lm_robust(per503~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i,weights=pervote)
#plots (median cty):
# tikz(paste0("plot_","LMp3_m4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Equality, Positive") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(0.0001,15)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(15-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_per) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_m4,-4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_m4,-4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_m4,-4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_m4,-4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_m4,-4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_m4,-4,6)) )
# dev.off()
LMp3_m4$r.squared
LMp3_m4$nobs
c(LMp3_m4$coefficients["time2"],LMp3_m4$std.error["time2"],LMp3_m4$p.value["time2"])#t2-t1=time2
Ztestfun(LMp3_m4,2,3)
Ztestfun(LMp3_m4,3,4)
Ztestfun(LMp3_m4,4,5)
Ztestfun(LMp3_m4,5,6)


##starting value for thresholds -2
#time variables:
t0=-2#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
Lp3_m2=lm(per503~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,weights=pervote)
LMp3_m2=lm_robust(per503~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i,weights=pervote)
#plots (median cty):
# tikz(paste0("plot_","LMp3_m2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Equality, Positive") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(0.0001,15)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(15-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_per) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_m2,-2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_m2,-2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_m2,-2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_m2,-2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_m2,-2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_m2,-2,6)) )
# dev.off()
LMp3_m2$r.squared
LMp3_m2$nobs
c(LMp3_m2$coefficients["time2"],LMp3_m2$std.error["time2"],LMp3_m2$p.value["time2"])#t2-t1=time2
Ztestfun(LMp3_m2,2,3)
Ztestfun(LMp3_m2,3,4)
Ztestfun(LMp3_m2,4,5)
Ztestfun(LMp3_m2,5,6)


##starting value for thresholds 2
#time variables:
t0=2#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
Lp3_2=lm(per503~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,weights=pervote)
LMp3_2=lm_robust(per503~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i,weights=pervote)
#plots (median cty):
# tikz(paste0("plot_","LMp3_2.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Equality, Positive") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(0.0001,15)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(15-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_per) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_2,2,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_2,2,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_2,2,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_2,2,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_2,2,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_2,2,6)) )
# dev.off()
LMp3_2$r.squared
LMp3_2$nobs
c(LMp3_2$coefficients["time2"],LMp3_2$std.error["time2"],LMp3_2$p.value["time2"])#t2-t1=time2
Ztestfun(LMp3_2,2,3)
Ztestfun(LMp3_2,3,4)
Ztestfun(LMp3_2,4,5)
Ztestfun(LMp3_2,5,6)


##starting value for thresholds 4
#time variables:
t0=4#starting value for thresholds (-4,-2,0,2,4)
# dc$time1=ifelse(dc$time<=10+t0,1,0)
dc$time2=ifelse(dc$time>10+t0&dc$time<=20+t0,1,0)
dc$time3=ifelse(dc$time>20+t0&dc$time<=30+t0,1,0)
dc$time4=ifelse(dc$time>30+t0&dc$time<=40+t0,1,0)
dc$time5=ifelse(dc$time>40+t0&dc$time<=50+t0,1,0)
dc$time6=ifelse(dc$time>50+t0,1,0)
#regression:
Lp3_4=lm(per503~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,weights=pervote)
LMp3_4=lm_robust(per503~time2+time3+time4+time5+time6+factor(Ec)-1,data=dc,cluster=i,weights=pervote)
#plots (median cty):
# tikz(paste0("plot_","LMp3_4.tex"),width=4, height=3)
plot( ggplot() +ggtitle("Equality, Positive") +xlab("Year") +ylab("estimated share") + coord_cartesian(ylim=c(0.0001,15)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(15-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dydc_per) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_4,4,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_4,4,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_4,4,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_4,4,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_4,4,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LM_dc(LMp3_4,4,6)) )
# dev.off()
LMp3_4$r.squared
LMp3_4$nobs
c(LMp3_4$coefficients["time2"],LMp3_4$std.error["time2"],LMp3_4$p.value["time2"])#t2-t1=time2
Ztestfun(LMp3_4,2,3)
Ztestfun(LMp3_4,3,4)
Ztestfun(LMp3_4,4,5)
Ztestfun(LMp3_4,5,6)


##Table I11##
##table full results:
stargazer(Lp3_m4,Lp3_m2,L_dc_10_per503,Lp3_2,Lp3_4,
          se = list(LMp3_m4$std.error,LMp3_m2$std.error,LM_dc_10_per503$std.error,LMp3_2$std.error,LMp3_4$std.error))


#RC WITH DISTANCE VOTER-PARTY - 10Y SEGMENTS:----

#confidence interval functions:
CI.PlLllseslrd10fun=function(CLC,t0,pe){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    # t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    CI.PlLl[t,1]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4-
      (CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+
      1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]-
      2*1*1*vcov(CLC)["Pl_ISL","Ll_ISL"]+2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[(1+t0):(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  # CI.PlLl=CI.PlLl[19:59,]
  CI.PlLl
}

#Z-test functions:
ZtestfunT12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunT=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTT12=function(CLC1,CLC2){
  m=(CLC1$coefficients[paste0("Pl:time",2)]-CLC1$coefficients[paste0("Ll:time",2)]-
       CLC2$coefficients[paste0("Pl:time",2)]+CLC2$coefficients[paste0("Ll:time",2)])#mean
  v=vcov(CLC1)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC1)[paste0("Ll:time",2),paste0("Ll:time",2)]+
    vcov(CLC2)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC2)[paste0("Ll:time",2),paste0("Ll:time",2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTT=function(CLC1,CLC2,per1,per2){
  m=-(CLC1$coefficients[paste0("Pl:time",per1)]-CLC1$coefficients[paste0("Ll:time",per1)]-
        CLC2$coefficients[paste0("Pl:time",per1)]+CLC2$coefficients[paste0("Ll:time",per1)]-
        CLC1$coefficients[paste0("Pl:time",per2)]+CLC1$coefficients[paste0("Ll:time",per2)]+
        CLC2$coefficients[paste0("Pl:time",per2)]-CLC2$coefficients[paste0("Ll:time",per2)])#mean
  v=vcov(CLC1)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC1)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC2)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC2)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC1)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC1)[paste0("Ll:time",per2),paste0("Ll:time",per2)]+
    vcov(CLC2)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC2)[paste0("Ll:time",per2),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}

#time variables:
t0=20#starting value for thresholds (16,18,20,22,24)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0,1,0)

#regressions:
CLC_lseslrd10_20=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+LRD
                      +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                      +strata(Esalt) ,robust=T ,data=de[!is.na(de$LRD),], weights=Rwd, method="efron")#regression
names(CLC_lseslrd10_20$coefficients)[names(CLC_lseslrd10_20$coefficients)=="time2:Ll"]="Ll:time2"

CLC_lseslrd10_20nlrd=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4
                        +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                        +strata(Esalt) ,robust=T ,data=de[!is.na(de$LRD),], weights=Rwd, method="efron")#regression
names(CLC_lseslrd10_20nlrd$coefficients)[names(CLC_lseslrd10_20nlrd$coefficients)=="time2:Ll"]="Ll:time2"

#plots:
#median cty:
# tikz(paste0("plot_","CLC_lseslrd10","_PlLl_20.tex"),width=4, height=3)#plot Pl-Ll:
plot( ggplot() 
      +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyLRD) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="blue", lwd=2, CI.PlLllseslrd10fun(CLC_lseslrd10_20,20,1)) +#w/ LRD
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="blue", lwd=2, CI.PlLllseslrd10fun(CLC_lseslrd10_20,20,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="blue", lwd=2, CI.PlLllseslrd10fun(CLC_lseslrd10_20,20,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="blue", lwd=2, CI.PlLllseslrd10fun(CLC_lseslrd10_20,20,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="green4", lty="44", lwd=3, CI.PlLllseslrd10fun(CLC_lseslrd10_20nlrd,20,1)) +#w/o LRD
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="green4", lty="44", lwd=3, CI.PlLllseslrd10fun(CLC_lseslrd10_20nlrd,20,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="green4", lty="44", lwd=3, CI.PlLllseslrd10fun(CLC_lseslrd10_20nlrd,20,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="green4", lty="44", lwd=3, CI.PlLllseslrd10fun(CLC_lseslrd10_20nlrd,20,4)) )
# dev.off()


##Table E1## and ##Table I13##
#table:
stargazer(CLC_lseslrd10_20,CLC_lseslrd10_20nlrd)
ZtestfunT12(CLC_lseslrd10_20)
ZtestfunT(CLC_lseslrd10_20,2,3)
ZtestfunT(CLC_lseslrd10_20,3,4)
ZtestfunT12(CLC_lseslrd10_20nlrd)
ZtestfunT(CLC_lseslrd10_20nlrd,2,3)
ZtestfunT(CLC_lseslrd10_20nlrd,3,4)
ZtestfunTT12(CLC_lseslrd10_20,CLC_lseslrd10_20nlrd)
ZtestfunTT(CLC_lseslrd10_20,CLC_lseslrd10_20nlrd,2,3)
ZtestfunTT(CLC_lseslrd10_20,CLC_lseslrd10_20nlrd,3,4)


#MEDIA - USE OF MEDIA OVER TIME:----

#data availability:
sort(unique(de$Ey[!(is.na(de$Mnwsp))]))#1979-2013
sort(unique(de$Ey[!(is.na(de$Mradi))]))#1979-2011
sort(unique(de$Ey[!(is.na(de$Mtele))]))#1979-2013
sort(unique(de$Ey[!(is.na(de$Minte))]))#2003:2013
#even if there are up to two elections before 1979, I exclude them since there are
# not enough data before 1979 and the two elections could over-influence the trend
# before 1979; however by keeping them the trends substantially do not change.

#base for density plots:
#create dataset density all data:
dyMnwsp = de[!is.na(de$Va)&!is.na(de$Pl)&!is.na(de$Ll)&!is.na(de$Mnwsp),] %>% group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dyMnwsp$Eyn=dyMnwsp$Eyn/1020288#density by Ey
dyMnwsp=dyMnwsp %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey
dyMradi = de[!is.na(de$Va)&!is.na(de$Pl)&!is.na(de$Ll)&!is.na(de$Mradi),] %>% group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dyMradi$Eyn=dyMradi$Eyn/1020288#density by Ey
dyMradi=dyMradi %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey
dyMtele = de[!is.na(de$Va)&!is.na(de$Pl)&!is.na(de$Ll)&!is.na(de$Mtele),] %>% group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dyMtele$Eyn=dyMtele$Eyn/1020288#density by Ey
dyMtele=dyMtele %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey
dyMinte = de[!is.na(de$Va)&!is.na(de$Pl)&!is.na(de$Ll)&!is.na(de$Minte),] %>% group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dyMinte$Eyn=dyMinte$Eyn/1020288#density by Ey
dyMinte=dyMinte %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey

#confidence interval functions:
CI.LMMediafun=function(LM,t0){ #function creating Confidence Interval for Med
  CI.Med=matrix(NA,nrow=59,ncol=4)#Med:
  for (t in 1:59) {
    t3=pmin(t,30+t0)
    t4=pmax(30+t0,pmin(t,40+t0))
    t5=pmax(40+t0,t)
    CI.Med[t,1]=LM$coefficients["factor(Ec)DEU"]+LM$coefficients["time3"]*t3+LM$coefficients["time4"]*t4+LM$coefficients["time5"]*t5#fitted values
    CI.Med[t,2]=1*vcov(LM)["factor(Ec)DEU","factor(Ec)DEU"]+t3^2*vcov(LM)["time3","time3"]+t4^2*vcov(LM)["time4","time4"]+t5^2*vcov(LM)["time5","time5"]+
      2*1*t3*vcov(LM)["factor(Ec)DEU","time3"]+2*1*t4*vcov(LM)["factor(Ec)DEU","time4"]+2*1*t5*vcov(LM)["factor(Ec)DEU","time5"]+
      2*t3*t4*vcov(LM)["time3","time4"]+2*t3*t5*vcov(LM)["time3","time5"]+
      2*t4*t5*vcov(LM)["time4","time5"]#Variance
    CI.Med[t,3]=CI.Med[t,1]-qnorm(0.975)*sqrt(CI.Med[t,2])#95% CI lower
    CI.Med[t,4]=CI.Med[t,1]+qnorm(0.975)*sqrt(CI.Med[t,2])#95% CI upper
  }
  CI.Med=as.data.frame(CI.Med)
  CI.Med$t=c(1961:2019)
  CI.Med=CI.Med[19:53,]
  CI.Med
}
CI.LMMediaintefun=function(LM){ #function creating Confidence Interval for Med
  CI.Med=matrix(NA,nrow=59,ncol=4)#Med:
  for (t in 1:59) {
    CI.Med[t,1]=LM$coefficients["factor(Ec)DEU"]+LM$coefficients["time"]*t#fitted values
    CI.Med[t,2]=1*vcov(LM)["factor(Ec)DEU","factor(Ec)DEU"]+t^2*vcov(LM)["time","time"]+
      2*1*t*vcov(LM)["factor(Ec)DEU","time"]#Variance
    CI.Med[t,3]=CI.Med[t,1]-qnorm(0.975)*sqrt(CI.Med[t,2])#95% CI lower
    CI.Med[t,4]=CI.Med[t,1]+qnorm(0.975)*sqrt(CI.Med[t,2])#95% CI upper
  }
  CI.Med=as.data.frame(CI.Med)
  CI.Med$t=c(1961:2019)
  CI.Med=CI.Med[43:53,]
  CI.Med
}
CI.LMMediafun80=function(LM,t0){ #function creating Confidence Interval for Med
  CI.Med=matrix(NA,nrow=29,ncol=4)#Med:
  for (t in 1:29) {
    t3=pmin(t,10+t0)
    t4=pmax(10+t0,pmin(t,20+t0))
    t5=pmax(20+t0,t)
    CI.Med[t,1]=LM$coefficients["factor(Ec)DEU"]+LM$coefficients["time3"]*t3+LM$coefficients["time4"]*t4+LM$coefficients["time5"]*t5#fitted values
    CI.Med[t,2]=1*vcov(LM)["factor(Ec)DEU","factor(Ec)DEU"]+t3^2*vcov(LM)["time3","time3"]+t4^2*vcov(LM)["time4","time4"]+t5^2*vcov(LM)["time5","time5"]+
      2*1*t3*vcov(LM)["factor(Ec)DEU","time3"]+2*1*t4*vcov(LM)["factor(Ec)DEU","time4"]+2*1*t5*vcov(LM)["factor(Ec)DEU","time5"]+
      2*t3*t4*vcov(LM)["time3","time4"]+2*t3*t5*vcov(LM)["time3","time5"]+
      2*t4*t5*vcov(LM)["time4","time5"]#Variance
    CI.Med[t,3]=CI.Med[t,1]-qnorm(0.975)*sqrt(CI.Med[t,2])#95% CI lower
    CI.Med[t,4]=CI.Med[t,1]+qnorm(0.975)*sqrt(CI.Med[t,2])#95% CI upper
  }
  CI.Med=as.data.frame(CI.Med)
  CI.Med$t=c(1981:2009)
  CI.Med
}
CI.LMMediafun80=function(LM,t0){ #function creating Confidence Interval for Med
  CI.Med=matrix(NA,nrow=29,ncol=4)#Med:
  for (t in 1:29) {
    t3=pmin(t,10+t0)
    t4=pmax(10+t0,pmin(t,20+t0))
    t5=pmax(20+t0,t)
    CI.Med[t,1]=LM$coefficients["time3"]*t3+LM$coefficients["time4"]*t4+LM$coefficients["time5"]*t5#fitted values
    CI.Med[t,2]=t3^2*vcov(LM)["time3","time3"]+t4^2*vcov(LM)["time4","time4"]+t5^2*vcov(LM)["time5","time5"]+
      2*t3*t4*vcov(LM)["time3","time4"]+2*t3*t5*vcov(LM)["time3","time5"]+
      2*t4*t5*vcov(LM)["time4","time5"]#Variance
    CI.Med[t,3]=CI.Med[t,1]-qnorm(0.975)*sqrt(CI.Med[t,2])#95% CI lower
    CI.Med[t,4]=CI.Med[t,1]+qnorm(0.975)*sqrt(CI.Med[t,2])#95% CI upper
  }
  CI.Med=as.data.frame(CI.Med)
  CI.Med$t=c(1981:2009)
  CI.Med
}

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
de$time3=pmin(de$time,30+t0)
de$time4=pmax(30+t0,pmin(de$time,40+t0))
de$time5=pmax(40+t0,de$time)

#regressions all respondents:
LM_Mnwsp_0=lm_robust(Mnwsp ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
LM_Mradi_0=lm_robust(Mradi ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
LM_Mtele_0=lm_robust(Mtele ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
LM_Minte=lm_robust(Minte ~ time+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
L_Mnwsp_0=lm(Mnwsp ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression
L_Mradi_0=lm(Mradi ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression
L_Mtele_0=lm(Mtele ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression
L_Minte=lm(Minte ~ time+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression
sort(unique(de$Es[!is.na(de$Mtele)]))#Mtele from 42 election studies


##Figure 1##
#plots all respondents:
# tikz(paste0("plot_","LM_Mnwsp_0",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Newspaper") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMnwsp) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LM_Mnwsp_0,0)) )
# dev.off()
# tikz(paste0("plot_","LM_Mradi_0",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Radio") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMradi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LM_Mradi_0,0)) )
# dev.off()
# tikz(paste0("plot_","LM_Mtele_0",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Television") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMtele) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LM_Mtele_0,0)) )
# dev.off()
# tikz(paste0("plot_","LM_Minte",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Internet") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMinte) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediaintefun(LM_Minte)) )
# dev.off()


##MEDIA - USE OF MEDIA OVER TIME - ALTERNATIVE THRESHOLDS:----

##starting value for thresholds -4
#time variables:
t0=-4#starting value for thresholds (-4,-2,0,2,4)
de$time3=pmin(de$time,30+t0)
de$time4=pmax(30+t0,pmin(de$time,40+t0))
de$time5=pmax(40+t0,de$time)

#regressions all respondents:
LMn_m4=lm_robust(Mnwsp ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
LMr_m4=lm_robust(Mradi ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
LMt_m4=lm_robust(Mtele ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
Ln_m4=lm(Mnwsp ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression
Lr_m4=lm(Mradi ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression
Lt_m4=lm(Mtele ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression

#plots all respondents:
# tikz(paste0("plot_","LM_Mnwsp_m4",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Newspaper") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMnwsp) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMn_m4,-4)) )
# dev.off()
# tikz(paste0("plot_","LM_Mradi_m4",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Radio") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMradi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMr_m4,-4)) )
# dev.off()
# tikz(paste0("plot_","LM_Mtele_m4",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Television") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMtele) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMt_m4,-4)) )
# dev.off()


##starting value for thresholds -2
#time variables:
t0=-2#starting value for thresholds (-4,-2,0,2,4)
de$time3=pmin(de$time,30+t0)
de$time4=pmax(30+t0,pmin(de$time,40+t0))
de$time5=pmax(40+t0,de$time)

#regressions all respondents:
LMn_m2=lm_robust(Mnwsp ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
LMr_m2=lm_robust(Mradi ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
LMt_m2=lm_robust(Mtele ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
Ln_m2=lm(Mnwsp ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression
Lr_m2=lm(Mradi ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression
Lt_m2=lm(Mtele ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression

#plots all respondents:
# tikz(paste0("plot_","LM_Mnwsp_m2",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Newspaper") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMnwsp) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMn_m2,-2)) )
# dev.off()
# tikz(paste0("plot_","LM_Mradi_m2",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Radio") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMradi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMr_m2,-2)) )
# dev.off()
# tikz(paste0("plot_","LM_Mtele_m2",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Television") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMtele) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMt_m2,-2)) )
# dev.off()


##starting value for thresholds 2
#time variables:
t0=2#starting value for thresholds (-4,-2,0,2,4)
de$time3=pmin(de$time,30+t0)
de$time4=pmax(30+t0,pmin(de$time,40+t0))
de$time5=pmax(40+t0,de$time)

#regressions all respondents:
LMn_2=lm_robust(Mnwsp ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
LMr_2=lm_robust(Mradi ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
LMt_2=lm_robust(Mtele ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
Ln_2=lm(Mnwsp ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression
Lr_2=lm(Mradi ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression
Lt_2=lm(Mtele ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression

#plots all respondents:
# tikz(paste0("plot_","LM_Mnwsp_2",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Newspaper") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMnwsp) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMn_2,2)) )
# dev.off()
# tikz(paste0("plot_","LM_Mradi_2",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Radio") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMradi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMr_2,2)) )
# dev.off()
# tikz(paste0("plot_","LM_Mtele_2",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Television") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMtele) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMt_2,2)) )
# dev.off()


##starting value for thresholds 4
#time variables:
t0=4#starting value for thresholds (-4,-2,0,2,4)
de$time3=pmin(de$time,30+t0)
de$time4=pmax(30+t0,pmin(de$time,40+t0))
de$time5=pmax(40+t0,de$time)

#regressions all respondents:
LMn_4=lm_robust(Mnwsp ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
LMr_4=lm_robust(Mradi ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
LMt_4=lm_robust(Mtele ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
Ln_4=lm(Mnwsp ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression
Lr_4=lm(Mradi ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression
Lt_4=lm(Mtele ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression

#plots all respondents:
# tikz(paste0("plot_","LM_Mnwsp_4",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Newspaper") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMnwsp) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMn_4,4)) )
# dev.off()
# tikz(paste0("plot_","LM_Mradi_4",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Radio") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMradi) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMr_4,4)) )
# dev.off()
# tikz(paste0("plot_","LM_Mtele_4",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Television") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(0.0001,1)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(1-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyMtele) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMt_4,4)) )
# dev.off()


##table full results:
##Table D5##
stargazer(Ln_m4,Ln_m2,L_Mnwsp_0,Ln_2,Ln_4,#Mnwsp
          se = list(LMn_m4$std.error,LMn_m2$std.error,LM_Mnwsp_0$std.error,LMn_2$std.error,LMn_4$std.error))
##Table D6##
stargazer(Lr_m4,Lr_m2,L_Mradi_0,Lr_2,Lr_4,#Mradi
          se = list(LMr_m4$std.error,LMr_m2$std.error,LM_Mradi_0$std.error,LMr_2$std.error,LMr_4$std.error))
##Table D7##
stargazer(Lt_m4,Lt_m2,L_Mtele_0,Lt_2,Lt_4,#Mtele
          se = list(LMt_m4$std.error,LMt_m2$std.error,LM_Mtele_0$std.error,LMt_2$std.error,LMt_4$std.error))


#MEDIA - EFFECT OF LIKABILITY * MEDIA USE:----

#regressions with election-slope FE:

#prepare variables:
#Pl:Es FE:
for (i1 in 1:length(sort(unique(de$Es)))) {
  de[[paste0("Pl_",sort(unique(de$Es))[i1])]]=ifelse(de$Es==sort(unique(de$Es))[i1],de$Pl,0)
}
#Ll:Es FE:
for (i1 in 1:length(sort(unique(de$Es)))) {
  de[[paste0("Ll_",sort(unique(de$Es))[i1])]]=ifelse(de$Es==sort(unique(de$Es))[i1],de$Ll,0)
}

#regressions:
# (+Pl+Ll already in due to the country-slope FE)
# (Mnwsp alone cannot be estimated because it is a multinomial model and it is constant across choices)
CLC_nwsp_=clogit(Va ~ Pl:Mnwsp+Ll:Mnwsp
                 +Pl_AUS_1993+Ll_AUS_1993+Pl_AUS_1996+Ll_AUS_1996+Pl_AUS_1998+Ll_AUS_1998+Pl_AUS_2001+Ll_AUS_2001+Pl_AUS_2004+Ll_AUS_2004+Pl_AUS_2007+Ll_AUS_2007+Pl_AUS_2013+Ll_AUS_2013+Pl_AUS_2019+Ll_AUS_2019+Pl_AUT_2013+Ll_AUT_2013+Pl_AUT_2017+Ll_AUT_2017+Pl_CAN_1968+Ll_CAN_1968+Pl_CAN_1974+Ll_CAN_1974+Pl_CAN_1979+Ll_CAN_1979+Pl_CAN_1988+Ll_CAN_1988+Pl_CAN_1993+Ll_CAN_1993+Pl_CAN_1997+Ll_CAN_1997+Pl_CAN_2008+Ll_CAN_2008+Pl_CAN_2011+Ll_CAN_2011+Pl_CAN_2015+Ll_CAN_2015+Pl_DEU_1961+Ll_DEU_1961+Pl_DEU_1965+Ll_DEU_1965+Pl_DEU_1976+Ll_DEU_1976+Pl_DEU_1980+Ll_DEU_1980+Pl_DEU_1983+Ll_DEU_1983+Pl_DEU_1987+Ll_DEU_1987+Pl_DEU_1990+Ll_DEU_1990+Pl_DEU_1994+Ll_DEU_1994+Pl_DEU_1998+Ll_DEU_1998+Pl_DEU_2005+Ll_DEU_2005+Pl_DEU_2009+Ll_DEU_2009+Pl_DEU_2013+Ll_DEU_2013+Pl_DEU_2017+Ll_DEU_2017+Pl_DNK_1971+Ll_DNK_1971+Pl_DNK_1973+Ll_DNK_1973+Pl_DNK_1994+Ll_DNK_1994+Pl_DNK_1998+Ll_DNK_1998+Pl_DNK_2007+Ll_DNK_2007+Pl_ESP_1979+Ll_ESP_1979+Pl_ESP_1982+Ll_ESP_1982+Pl_ESP_1993+Ll_ESP_1993+Pl_ESP_1996+Ll_ESP_1996+Pl_ESP_2000+Ll_ESP_2000+Pl_ESP_2004+Ll_ESP_2004+Pl_ESP_2008+Ll_ESP_2008+Pl_FIN_2003+Ll_FIN_2003+Pl_FIN_2007+Ll_FIN_2007+Pl_FIN_2011+Ll_FIN_2011+Pl_FIN_2015+Ll_FIN_2015+Pl_GBR_1974f+Ll_GBR_1974f+Pl_GBR_1974o+Ll_GBR_1974o+Pl_GBR_1979+Ll_GBR_1979+Pl_GBR_1997+Ll_GBR_1997+Pl_GBR_2001+Ll_GBR_2001+Pl_GBR_2005+Ll_GBR_2005+Pl_GBR_2015+Ll_GBR_2015+Pl_GRC_1985+Ll_GRC_1985+Pl_GRC_1989+Ll_GRC_1989+Pl_GRC_1990+Ll_GRC_1990+Pl_GRC_1996+Ll_GRC_1996+Pl_GRC_2000+Ll_GRC_2000+Pl_GRC_2004+Ll_GRC_2004+Pl_GRC_2007+Ll_GRC_2007+Pl_GRC_2009+Ll_GRC_2009+Pl_GRC_2012+Ll_GRC_2012+Pl_GRC_2015j+Ll_GRC_2015j+Pl_GRC_2015s+Ll_GRC_2015s+Pl_IRL_2002+Ll_IRL_2002+Pl_IRL_2007+Ll_IRL_2007+Pl_IRL_2011+Ll_IRL_2011+Pl_IRL_2016+Ll_IRL_2016+Pl_ISL_1987+Ll_ISL_1987+Pl_ISL_1991+Ll_ISL_1991+Pl_ISL_1995+Ll_ISL_1995+Pl_ISL_1999+Ll_ISL_1999+Pl_ISL_2003+Ll_ISL_2003+Pl_ISL_2007+Ll_ISL_2007+Pl_ISL_2009+Ll_ISL_2009+Pl_ISL_2013+Ll_ISL_2013+Pl_ISL_2016+Ll_ISL_2016+Pl_ISL_2017+Ll_ISL_2017+Pl_ISR_1996+Ll_ISR_1996+Pl_ISR_2006+Ll_ISR_2006+Pl_ISR_2013+Ll_ISR_2013+Pl_ITA_2006+Ll_ITA_2006+Pl_ITA_2008+Ll_ITA_2008+Pl_ITA_2018+Ll_ITA_2018+Pl_NLD_1986+Ll_NLD_1986+Pl_NLD_1994+Ll_NLD_1994+Pl_NLD_1998+Ll_NLD_1998+Pl_NLD_2002+Ll_NLD_2002+Pl_NLD_2006+Ll_NLD_2006+Pl_NLD_2010+Ll_NLD_2010+Pl_NOR_1981+Ll_NOR_1981+Pl_NOR_1985+Ll_NOR_1985+Pl_NOR_1989+Ll_NOR_1989+Pl_NOR_1993+Ll_NOR_1993+Pl_NOR_1997+Ll_NOR_1997+Pl_NOR_2001+Ll_NOR_2001+Pl_NOR_2005+Ll_NOR_2005+Pl_NOR_2009+Ll_NOR_2009+Pl_NOR_2013+Ll_NOR_2013+Pl_NOR_2017+Ll_NOR_2017+Pl_NZL_1996+Ll_NZL_1996+Pl_NZL_2008+Ll_NZL_2008+Pl_NZL_2011+Ll_NZL_2011+Pl_NZL_2014+Ll_NZL_2014+Pl_NZL_2017+Ll_NZL_2017+Pl_PRT_2002+Ll_PRT_2002+Pl_PRT_2005+Ll_PRT_2005+Pl_PRT_2009+Ll_PRT_2009+Pl_PRT_2015+Ll_PRT_2015+Pl_SWE_1979+Ll_SWE_1979+Pl_SWE_1982+Ll_SWE_1982+Pl_SWE_1985+Ll_SWE_1985+Pl_SWE_1988+Ll_SWE_1988+Pl_SWE_1991+Ll_SWE_1991+Pl_SWE_1994+Ll_SWE_1994+Pl_SWE_1998+Ll_SWE_1998+Pl_SWE_2002+Ll_SWE_2002+Pl_SWE_2006+Ll_SWE_2006+Pl_SWE_2010+Ll_SWE_2010+Pl_SWE_2014+Ll_SWE_2014
                 +strata(Esalt), robust=T, data=de, method="efron")
CLC_radi_=clogit(Va ~ Pl:Mradi+Ll:Mradi
                 +Pl_AUS_1993+Ll_AUS_1993+Pl_AUS_1996+Ll_AUS_1996+Pl_AUS_1998+Ll_AUS_1998+Pl_AUS_2001+Ll_AUS_2001+Pl_AUS_2004+Ll_AUS_2004+Pl_AUS_2007+Ll_AUS_2007+Pl_AUS_2013+Ll_AUS_2013+Pl_AUS_2019+Ll_AUS_2019+Pl_AUT_2013+Ll_AUT_2013+Pl_AUT_2017+Ll_AUT_2017+Pl_CAN_1968+Ll_CAN_1968+Pl_CAN_1974+Ll_CAN_1974+Pl_CAN_1979+Ll_CAN_1979+Pl_CAN_1988+Ll_CAN_1988+Pl_CAN_1993+Ll_CAN_1993+Pl_CAN_1997+Ll_CAN_1997+Pl_CAN_2008+Ll_CAN_2008+Pl_CAN_2011+Ll_CAN_2011+Pl_CAN_2015+Ll_CAN_2015+Pl_DEU_1961+Ll_DEU_1961+Pl_DEU_1965+Ll_DEU_1965+Pl_DEU_1976+Ll_DEU_1976+Pl_DEU_1980+Ll_DEU_1980+Pl_DEU_1983+Ll_DEU_1983+Pl_DEU_1987+Ll_DEU_1987+Pl_DEU_1990+Ll_DEU_1990+Pl_DEU_1994+Ll_DEU_1994+Pl_DEU_1998+Ll_DEU_1998+Pl_DEU_2005+Ll_DEU_2005+Pl_DEU_2009+Ll_DEU_2009+Pl_DEU_2013+Ll_DEU_2013+Pl_DEU_2017+Ll_DEU_2017+Pl_DNK_1971+Ll_DNK_1971+Pl_DNK_1973+Ll_DNK_1973+Pl_DNK_1994+Ll_DNK_1994+Pl_DNK_1998+Ll_DNK_1998+Pl_DNK_2007+Ll_DNK_2007+Pl_ESP_1979+Ll_ESP_1979+Pl_ESP_1982+Ll_ESP_1982+Pl_ESP_1993+Ll_ESP_1993+Pl_ESP_1996+Ll_ESP_1996+Pl_ESP_2000+Ll_ESP_2000+Pl_ESP_2004+Ll_ESP_2004+Pl_ESP_2008+Ll_ESP_2008+Pl_FIN_2003+Ll_FIN_2003+Pl_FIN_2007+Ll_FIN_2007+Pl_FIN_2011+Ll_FIN_2011+Pl_FIN_2015+Ll_FIN_2015+Pl_GBR_1974f+Ll_GBR_1974f+Pl_GBR_1974o+Ll_GBR_1974o+Pl_GBR_1979+Ll_GBR_1979+Pl_GBR_1997+Ll_GBR_1997+Pl_GBR_2001+Ll_GBR_2001+Pl_GBR_2005+Ll_GBR_2005+Pl_GBR_2015+Ll_GBR_2015+Pl_GRC_1985+Ll_GRC_1985+Pl_GRC_1989+Ll_GRC_1989+Pl_GRC_1990+Ll_GRC_1990+Pl_GRC_1996+Ll_GRC_1996+Pl_GRC_2000+Ll_GRC_2000+Pl_GRC_2004+Ll_GRC_2004+Pl_GRC_2007+Ll_GRC_2007+Pl_GRC_2009+Ll_GRC_2009+Pl_GRC_2012+Ll_GRC_2012+Pl_GRC_2015j+Ll_GRC_2015j+Pl_GRC_2015s+Ll_GRC_2015s+Pl_IRL_2002+Ll_IRL_2002+Pl_IRL_2007+Ll_IRL_2007+Pl_IRL_2011+Ll_IRL_2011+Pl_IRL_2016+Ll_IRL_2016+Pl_ISL_1987+Ll_ISL_1987+Pl_ISL_1991+Ll_ISL_1991+Pl_ISL_1995+Ll_ISL_1995+Pl_ISL_1999+Ll_ISL_1999+Pl_ISL_2003+Ll_ISL_2003+Pl_ISL_2007+Ll_ISL_2007+Pl_ISL_2009+Ll_ISL_2009+Pl_ISL_2013+Ll_ISL_2013+Pl_ISL_2016+Ll_ISL_2016+Pl_ISL_2017+Ll_ISL_2017+Pl_ISR_1996+Ll_ISR_1996+Pl_ISR_2006+Ll_ISR_2006+Pl_ISR_2013+Ll_ISR_2013+Pl_ITA_2006+Ll_ITA_2006+Pl_ITA_2008+Ll_ITA_2008+Pl_ITA_2018+Ll_ITA_2018+Pl_NLD_1986+Ll_NLD_1986+Pl_NLD_1994+Ll_NLD_1994+Pl_NLD_1998+Ll_NLD_1998+Pl_NLD_2002+Ll_NLD_2002+Pl_NLD_2006+Ll_NLD_2006+Pl_NLD_2010+Ll_NLD_2010+Pl_NOR_1981+Ll_NOR_1981+Pl_NOR_1985+Ll_NOR_1985+Pl_NOR_1989+Ll_NOR_1989+Pl_NOR_1993+Ll_NOR_1993+Pl_NOR_1997+Ll_NOR_1997+Pl_NOR_2001+Ll_NOR_2001+Pl_NOR_2005+Ll_NOR_2005+Pl_NOR_2009+Ll_NOR_2009+Pl_NOR_2013+Ll_NOR_2013+Pl_NOR_2017+Ll_NOR_2017+Pl_NZL_1996+Ll_NZL_1996+Pl_NZL_2008+Ll_NZL_2008+Pl_NZL_2011+Ll_NZL_2011+Pl_NZL_2014+Ll_NZL_2014+Pl_NZL_2017+Ll_NZL_2017+Pl_PRT_2002+Ll_PRT_2002+Pl_PRT_2005+Ll_PRT_2005+Pl_PRT_2009+Ll_PRT_2009+Pl_PRT_2015+Ll_PRT_2015+Pl_SWE_1979+Ll_SWE_1979+Pl_SWE_1982+Ll_SWE_1982+Pl_SWE_1985+Ll_SWE_1985+Pl_SWE_1988+Ll_SWE_1988+Pl_SWE_1991+Ll_SWE_1991+Pl_SWE_1994+Ll_SWE_1994+Pl_SWE_1998+Ll_SWE_1998+Pl_SWE_2002+Ll_SWE_2002+Pl_SWE_2006+Ll_SWE_2006+Pl_SWE_2010+Ll_SWE_2010+Pl_SWE_2014+Ll_SWE_2014
                 +strata(Esalt), robust=T, data=de, method="efron")
CLC_tele_=clogit(Va ~ Pl:Mtele+Ll:Mtele
                 +Pl_AUS_1993+Ll_AUS_1993+Pl_AUS_1996+Ll_AUS_1996+Pl_AUS_1998+Ll_AUS_1998+Pl_AUS_2001+Ll_AUS_2001+Pl_AUS_2004+Ll_AUS_2004+Pl_AUS_2007+Ll_AUS_2007+Pl_AUS_2013+Ll_AUS_2013+Pl_AUS_2019+Ll_AUS_2019+Pl_AUT_2013+Ll_AUT_2013+Pl_AUT_2017+Ll_AUT_2017+Pl_CAN_1968+Ll_CAN_1968+Pl_CAN_1974+Ll_CAN_1974+Pl_CAN_1979+Ll_CAN_1979+Pl_CAN_1988+Ll_CAN_1988+Pl_CAN_1993+Ll_CAN_1993+Pl_CAN_1997+Ll_CAN_1997+Pl_CAN_2008+Ll_CAN_2008+Pl_CAN_2011+Ll_CAN_2011+Pl_CAN_2015+Ll_CAN_2015+Pl_DEU_1961+Ll_DEU_1961+Pl_DEU_1965+Ll_DEU_1965+Pl_DEU_1976+Ll_DEU_1976+Pl_DEU_1980+Ll_DEU_1980+Pl_DEU_1983+Ll_DEU_1983+Pl_DEU_1987+Ll_DEU_1987+Pl_DEU_1990+Ll_DEU_1990+Pl_DEU_1994+Ll_DEU_1994+Pl_DEU_1998+Ll_DEU_1998+Pl_DEU_2005+Ll_DEU_2005+Pl_DEU_2009+Ll_DEU_2009+Pl_DEU_2013+Ll_DEU_2013+Pl_DEU_2017+Ll_DEU_2017+Pl_DNK_1971+Ll_DNK_1971+Pl_DNK_1973+Ll_DNK_1973+Pl_DNK_1994+Ll_DNK_1994+Pl_DNK_1998+Ll_DNK_1998+Pl_DNK_2007+Ll_DNK_2007+Pl_ESP_1979+Ll_ESP_1979+Pl_ESP_1982+Ll_ESP_1982+Pl_ESP_1993+Ll_ESP_1993+Pl_ESP_1996+Ll_ESP_1996+Pl_ESP_2000+Ll_ESP_2000+Pl_ESP_2004+Ll_ESP_2004+Pl_ESP_2008+Ll_ESP_2008+Pl_FIN_2003+Ll_FIN_2003+Pl_FIN_2007+Ll_FIN_2007+Pl_FIN_2011+Ll_FIN_2011+Pl_FIN_2015+Ll_FIN_2015+Pl_GBR_1974f+Ll_GBR_1974f+Pl_GBR_1974o+Ll_GBR_1974o+Pl_GBR_1979+Ll_GBR_1979+Pl_GBR_1997+Ll_GBR_1997+Pl_GBR_2001+Ll_GBR_2001+Pl_GBR_2005+Ll_GBR_2005+Pl_GBR_2015+Ll_GBR_2015+Pl_GRC_1985+Ll_GRC_1985+Pl_GRC_1989+Ll_GRC_1989+Pl_GRC_1990+Ll_GRC_1990+Pl_GRC_1996+Ll_GRC_1996+Pl_GRC_2000+Ll_GRC_2000+Pl_GRC_2004+Ll_GRC_2004+Pl_GRC_2007+Ll_GRC_2007+Pl_GRC_2009+Ll_GRC_2009+Pl_GRC_2012+Ll_GRC_2012+Pl_GRC_2015j+Ll_GRC_2015j+Pl_GRC_2015s+Ll_GRC_2015s+Pl_IRL_2002+Ll_IRL_2002+Pl_IRL_2007+Ll_IRL_2007+Pl_IRL_2011+Ll_IRL_2011+Pl_IRL_2016+Ll_IRL_2016+Pl_ISL_1987+Ll_ISL_1987+Pl_ISL_1991+Ll_ISL_1991+Pl_ISL_1995+Ll_ISL_1995+Pl_ISL_1999+Ll_ISL_1999+Pl_ISL_2003+Ll_ISL_2003+Pl_ISL_2007+Ll_ISL_2007+Pl_ISL_2009+Ll_ISL_2009+Pl_ISL_2013+Ll_ISL_2013+Pl_ISL_2016+Ll_ISL_2016+Pl_ISL_2017+Ll_ISL_2017+Pl_ISR_1996+Ll_ISR_1996+Pl_ISR_2006+Ll_ISR_2006+Pl_ISR_2013+Ll_ISR_2013+Pl_ITA_2006+Ll_ITA_2006+Pl_ITA_2008+Ll_ITA_2008+Pl_ITA_2018+Ll_ITA_2018+Pl_NLD_1986+Ll_NLD_1986+Pl_NLD_1994+Ll_NLD_1994+Pl_NLD_1998+Ll_NLD_1998+Pl_NLD_2002+Ll_NLD_2002+Pl_NLD_2006+Ll_NLD_2006+Pl_NLD_2010+Ll_NLD_2010+Pl_NOR_1981+Ll_NOR_1981+Pl_NOR_1985+Ll_NOR_1985+Pl_NOR_1989+Ll_NOR_1989+Pl_NOR_1993+Ll_NOR_1993+Pl_NOR_1997+Ll_NOR_1997+Pl_NOR_2001+Ll_NOR_2001+Pl_NOR_2005+Ll_NOR_2005+Pl_NOR_2009+Ll_NOR_2009+Pl_NOR_2013+Ll_NOR_2013+Pl_NOR_2017+Ll_NOR_2017+Pl_NZL_1996+Ll_NZL_1996+Pl_NZL_2008+Ll_NZL_2008+Pl_NZL_2011+Ll_NZL_2011+Pl_NZL_2014+Ll_NZL_2014+Pl_NZL_2017+Ll_NZL_2017+Pl_PRT_2002+Ll_PRT_2002+Pl_PRT_2005+Ll_PRT_2005+Pl_PRT_2009+Ll_PRT_2009+Pl_PRT_2015+Ll_PRT_2015+Pl_SWE_1979+Ll_SWE_1979+Pl_SWE_1982+Ll_SWE_1982+Pl_SWE_1985+Ll_SWE_1985+Pl_SWE_1988+Ll_SWE_1988+Pl_SWE_1991+Ll_SWE_1991+Pl_SWE_1994+Ll_SWE_1994+Pl_SWE_1998+Ll_SWE_1998+Pl_SWE_2002+Ll_SWE_2002+Pl_SWE_2006+Ll_SWE_2006+Pl_SWE_2010+Ll_SWE_2010+Pl_SWE_2014+Ll_SWE_2014
                 +strata(Esalt), robust=T, data=de, method="efron")
CLC_inte_=clogit(Va ~ Pl:Minte+Ll:Minte
                 +Pl_AUS_1993+Ll_AUS_1993+Pl_AUS_1996+Ll_AUS_1996+Pl_AUS_1998+Ll_AUS_1998+Pl_AUS_2001+Ll_AUS_2001+Pl_AUS_2004+Ll_AUS_2004+Pl_AUS_2007+Ll_AUS_2007+Pl_AUS_2013+Ll_AUS_2013+Pl_AUS_2019+Ll_AUS_2019+Pl_AUT_2013+Ll_AUT_2013+Pl_AUT_2017+Ll_AUT_2017+Pl_CAN_1968+Ll_CAN_1968+Pl_CAN_1974+Ll_CAN_1974+Pl_CAN_1979+Ll_CAN_1979+Pl_CAN_1988+Ll_CAN_1988+Pl_CAN_1993+Ll_CAN_1993+Pl_CAN_1997+Ll_CAN_1997+Pl_CAN_2008+Ll_CAN_2008+Pl_CAN_2011+Ll_CAN_2011+Pl_CAN_2015+Ll_CAN_2015+Pl_DEU_1961+Ll_DEU_1961+Pl_DEU_1965+Ll_DEU_1965+Pl_DEU_1976+Ll_DEU_1976+Pl_DEU_1980+Ll_DEU_1980+Pl_DEU_1983+Ll_DEU_1983+Pl_DEU_1987+Ll_DEU_1987+Pl_DEU_1990+Ll_DEU_1990+Pl_DEU_1994+Ll_DEU_1994+Pl_DEU_1998+Ll_DEU_1998+Pl_DEU_2005+Ll_DEU_2005+Pl_DEU_2009+Ll_DEU_2009+Pl_DEU_2013+Ll_DEU_2013+Pl_DEU_2017+Ll_DEU_2017+Pl_DNK_1971+Ll_DNK_1971+Pl_DNK_1973+Ll_DNK_1973+Pl_DNK_1994+Ll_DNK_1994+Pl_DNK_1998+Ll_DNK_1998+Pl_DNK_2007+Ll_DNK_2007+Pl_ESP_1979+Ll_ESP_1979+Pl_ESP_1982+Ll_ESP_1982+Pl_ESP_1993+Ll_ESP_1993+Pl_ESP_1996+Ll_ESP_1996+Pl_ESP_2000+Ll_ESP_2000+Pl_ESP_2004+Ll_ESP_2004+Pl_ESP_2008+Ll_ESP_2008+Pl_FIN_2003+Ll_FIN_2003+Pl_FIN_2007+Ll_FIN_2007+Pl_FIN_2011+Ll_FIN_2011+Pl_FIN_2015+Ll_FIN_2015+Pl_GBR_1974f+Ll_GBR_1974f+Pl_GBR_1974o+Ll_GBR_1974o+Pl_GBR_1979+Ll_GBR_1979+Pl_GBR_1997+Ll_GBR_1997+Pl_GBR_2001+Ll_GBR_2001+Pl_GBR_2005+Ll_GBR_2005+Pl_GBR_2015+Ll_GBR_2015+Pl_GRC_1985+Ll_GRC_1985+Pl_GRC_1989+Ll_GRC_1989+Pl_GRC_1990+Ll_GRC_1990+Pl_GRC_1996+Ll_GRC_1996+Pl_GRC_2000+Ll_GRC_2000+Pl_GRC_2004+Ll_GRC_2004+Pl_GRC_2007+Ll_GRC_2007+Pl_GRC_2009+Ll_GRC_2009+Pl_GRC_2012+Ll_GRC_2012+Pl_GRC_2015j+Ll_GRC_2015j+Pl_GRC_2015s+Ll_GRC_2015s+Pl_IRL_2002+Ll_IRL_2002+Pl_IRL_2007+Ll_IRL_2007+Pl_IRL_2011+Ll_IRL_2011+Pl_IRL_2016+Ll_IRL_2016+Pl_ISL_1987+Ll_ISL_1987+Pl_ISL_1991+Ll_ISL_1991+Pl_ISL_1995+Ll_ISL_1995+Pl_ISL_1999+Ll_ISL_1999+Pl_ISL_2003+Ll_ISL_2003+Pl_ISL_2007+Ll_ISL_2007+Pl_ISL_2009+Ll_ISL_2009+Pl_ISL_2013+Ll_ISL_2013+Pl_ISL_2016+Ll_ISL_2016+Pl_ISL_2017+Ll_ISL_2017+Pl_ISR_1996+Ll_ISR_1996+Pl_ISR_2006+Ll_ISR_2006+Pl_ISR_2013+Ll_ISR_2013+Pl_ITA_2006+Ll_ITA_2006+Pl_ITA_2008+Ll_ITA_2008+Pl_ITA_2018+Ll_ITA_2018+Pl_NLD_1986+Ll_NLD_1986+Pl_NLD_1994+Ll_NLD_1994+Pl_NLD_1998+Ll_NLD_1998+Pl_NLD_2002+Ll_NLD_2002+Pl_NLD_2006+Ll_NLD_2006+Pl_NLD_2010+Ll_NLD_2010+Pl_NOR_1981+Ll_NOR_1981+Pl_NOR_1985+Ll_NOR_1985+Pl_NOR_1989+Ll_NOR_1989+Pl_NOR_1993+Ll_NOR_1993+Pl_NOR_1997+Ll_NOR_1997+Pl_NOR_2001+Ll_NOR_2001+Pl_NOR_2005+Ll_NOR_2005+Pl_NOR_2009+Ll_NOR_2009+Pl_NOR_2013+Ll_NOR_2013+Pl_NOR_2017+Ll_NOR_2017+Pl_NZL_1996+Ll_NZL_1996+Pl_NZL_2008+Ll_NZL_2008+Pl_NZL_2011+Ll_NZL_2011+Pl_NZL_2014+Ll_NZL_2014+Pl_NZL_2017+Ll_NZL_2017+Pl_PRT_2002+Ll_PRT_2002+Pl_PRT_2005+Ll_PRT_2005+Pl_PRT_2009+Ll_PRT_2009+Pl_PRT_2015+Ll_PRT_2015+Pl_SWE_1979+Ll_SWE_1979+Pl_SWE_1982+Ll_SWE_1982+Pl_SWE_1985+Ll_SWE_1985+Pl_SWE_1988+Ll_SWE_1988+Pl_SWE_1991+Ll_SWE_1991+Pl_SWE_1994+Ll_SWE_1994+Pl_SWE_1998+Ll_SWE_1998+Pl_SWE_2002+Ll_SWE_2002+Pl_SWE_2006+Ll_SWE_2006+Pl_SWE_2010+Ll_SWE_2010+Pl_SWE_2014+Ll_SWE_2014
                 +strata(Esalt), robust=T, data=de, method="efron")

#change names for stargazer:
names(CLC_nwsp_$coefficients)[names(CLC_nwsp_$coefficients)=="Pl:Mnwsp"]="Party Likability * Media Use"
names(CLC_nwsp_$coefficients)[names(CLC_nwsp_$coefficients)=="Mnwsp:Ll"]="Leader Likability * Media Use"
names(CLC_radi_$coefficients)[names(CLC_radi_$coefficients)=="Pl:Mradi"]="Party Likability * Media Use"
names(CLC_radi_$coefficients)[names(CLC_radi_$coefficients)=="Mradi:Ll"]="Leader Likability * Media Use"
names(CLC_tele_$coefficients)[names(CLC_tele_$coefficients)=="Pl:Mtele"]="Party Likability * Media Use"
names(CLC_tele_$coefficients)[names(CLC_tele_$coefficients)=="Mtele:Ll"]="Leader Likability * Media Use"
names(CLC_inte_$coefficients)[names(CLC_inte_$coefficients)=="Pl:Minte"]="Party Likability * Media Use"
names(CLC_inte_$coefficients)[names(CLC_inte_$coefficients)=="Minte:Ll"]="Leader Likability * Media Use"

##Table 3##
stargazer(CLC_nwsp_,CLC_radi_,CLC_tele_,CLC_inte_)


##MEDIA - RC MEASURING MTELE-MNWSP:----

#confidence interval functions:
CI.LMMediafun=function(LM,t0){ #function creating Confidence Interval for Med
  CI.Med=matrix(NA,nrow=59,ncol=4)#Med:
  for (t in 1:59) {
    t3=pmin(t,30+t0)
    t4=pmax(30+t0,pmin(t,40+t0))
    t5=pmax(40+t0,t)
    CI.Med[t,1]=LM$coefficients["factor(Ec)DEU"]+LM$coefficients["time3"]*t3+LM$coefficients["time4"]*t4+LM$coefficients["time5"]*t5#fitted values
    CI.Med[t,2]=1*vcov(LM)["factor(Ec)DEU","factor(Ec)DEU"]+t3^2*vcov(LM)["time3","time3"]+t4^2*vcov(LM)["time4","time4"]+t5^2*vcov(LM)["time5","time5"]+
      2*1*t3*vcov(LM)["factor(Ec)DEU","time3"]+2*1*t4*vcov(LM)["factor(Ec)DEU","time4"]+2*1*t5*vcov(LM)["factor(Ec)DEU","time5"]+
      2*t3*t4*vcov(LM)["time3","time4"]+2*t3*t5*vcov(LM)["time3","time5"]+
      2*t4*t5*vcov(LM)["time4","time5"]#Variance
    CI.Med[t,3]=CI.Med[t,1]-qnorm(0.975)*sqrt(CI.Med[t,2])#95% CI lower
    CI.Med[t,4]=CI.Med[t,1]+qnorm(0.975)*sqrt(CI.Med[t,2])#95% CI upper
  }
  CI.Med=as.data.frame(CI.Med)
  CI.Med$t=c(1961:2019)
  CI.Med=CI.Med[19:53,]
  CI.Med
}

#create variable Mtele-Mnwsp:
de$Mtesp=de$Mtele-de$Mnwsp

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
de$time3=pmin(de$time,30+t0)
de$time4=pmax(30+t0,pmin(de$time,40+t0))
de$time5=pmax(40+t0,de$time)

#regressions all respondents:
LM_Mtesp_0=lm_robust(Mtesp ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
L_Mtesp_0=lm(Mtesp ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression


##Figure 2##
#plots all respondents:
# tikz(paste0("plot_","LM_Mtesp_0",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Television $-$ Newspaper") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(-0.4999,0.5)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        # geom_histogram(aes(x=Ey,y=..density..),binwidth = 1,color="gray85",fill="gray85",data=de[!is.na(de$Mtesp),]) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LM_Mtesp_0,0)) )
# dev.off()


##table (regressions with election-slope FE):

#prepare variables:
#Pl:Es FE:
for (i1 in 1:length(sort(unique(de$Es)))) {
  de[[paste0("Pl_",sort(unique(de$Es))[i1])]]=ifelse(de$Es==sort(unique(de$Es))[i1],de$Pl,0)
}
#Ll:Es FE:
for (i1 in 1:length(sort(unique(de$Es)))) {
  de[[paste0("Ll_",sort(unique(de$Es))[i1])]]=ifelse(de$Es==sort(unique(de$Es))[i1],de$Ll,0)
}

#regressions:
CLC_tesp_=clogit(Va ~ Pl:Mtesp+Ll:Mtesp
                 +Pl_AUS_1993+Ll_AUS_1993+Pl_AUS_1996+Ll_AUS_1996+Pl_AUS_1998+Ll_AUS_1998+Pl_AUS_2001+Ll_AUS_2001+Pl_AUS_2004+Ll_AUS_2004+Pl_AUS_2007+Ll_AUS_2007+Pl_AUS_2013+Ll_AUS_2013+Pl_AUS_2019+Ll_AUS_2019+Pl_AUT_2013+Ll_AUT_2013+Pl_AUT_2017+Ll_AUT_2017+Pl_CAN_1968+Ll_CAN_1968+Pl_CAN_1974+Ll_CAN_1974+Pl_CAN_1979+Ll_CAN_1979+Pl_CAN_1988+Ll_CAN_1988+Pl_CAN_1993+Ll_CAN_1993+Pl_CAN_1997+Ll_CAN_1997+Pl_CAN_2008+Ll_CAN_2008+Pl_CAN_2011+Ll_CAN_2011+Pl_CAN_2015+Ll_CAN_2015+Pl_DEU_1961+Ll_DEU_1961+Pl_DEU_1965+Ll_DEU_1965+Pl_DEU_1976+Ll_DEU_1976+Pl_DEU_1980+Ll_DEU_1980+Pl_DEU_1983+Ll_DEU_1983+Pl_DEU_1987+Ll_DEU_1987+Pl_DEU_1990+Ll_DEU_1990+Pl_DEU_1994+Ll_DEU_1994+Pl_DEU_1998+Ll_DEU_1998+Pl_DEU_2005+Ll_DEU_2005+Pl_DEU_2009+Ll_DEU_2009+Pl_DEU_2013+Ll_DEU_2013+Pl_DEU_2017+Ll_DEU_2017+Pl_DNK_1971+Ll_DNK_1971+Pl_DNK_1973+Ll_DNK_1973+Pl_DNK_1994+Ll_DNK_1994+Pl_DNK_1998+Ll_DNK_1998+Pl_DNK_2007+Ll_DNK_2007+Pl_ESP_1979+Ll_ESP_1979+Pl_ESP_1982+Ll_ESP_1982+Pl_ESP_1993+Ll_ESP_1993+Pl_ESP_1996+Ll_ESP_1996+Pl_ESP_2000+Ll_ESP_2000+Pl_ESP_2004+Ll_ESP_2004+Pl_ESP_2008+Ll_ESP_2008+Pl_FIN_2003+Ll_FIN_2003+Pl_FIN_2007+Ll_FIN_2007+Pl_FIN_2011+Ll_FIN_2011+Pl_FIN_2015+Ll_FIN_2015+Pl_GBR_1974f+Ll_GBR_1974f+Pl_GBR_1974o+Ll_GBR_1974o+Pl_GBR_1979+Ll_GBR_1979+Pl_GBR_1997+Ll_GBR_1997+Pl_GBR_2001+Ll_GBR_2001+Pl_GBR_2005+Ll_GBR_2005+Pl_GBR_2015+Ll_GBR_2015+Pl_GRC_1985+Ll_GRC_1985+Pl_GRC_1989+Ll_GRC_1989+Pl_GRC_1990+Ll_GRC_1990+Pl_GRC_1996+Ll_GRC_1996+Pl_GRC_2000+Ll_GRC_2000+Pl_GRC_2004+Ll_GRC_2004+Pl_GRC_2007+Ll_GRC_2007+Pl_GRC_2009+Ll_GRC_2009+Pl_GRC_2012+Ll_GRC_2012+Pl_GRC_2015j+Ll_GRC_2015j+Pl_GRC_2015s+Ll_GRC_2015s+Pl_IRL_2002+Ll_IRL_2002+Pl_IRL_2007+Ll_IRL_2007+Pl_IRL_2011+Ll_IRL_2011+Pl_IRL_2016+Ll_IRL_2016+Pl_ISL_1987+Ll_ISL_1987+Pl_ISL_1991+Ll_ISL_1991+Pl_ISL_1995+Ll_ISL_1995+Pl_ISL_1999+Ll_ISL_1999+Pl_ISL_2003+Ll_ISL_2003+Pl_ISL_2007+Ll_ISL_2007+Pl_ISL_2009+Ll_ISL_2009+Pl_ISL_2013+Ll_ISL_2013+Pl_ISL_2016+Ll_ISL_2016+Pl_ISL_2017+Ll_ISL_2017+Pl_ISR_1996+Ll_ISR_1996+Pl_ISR_2006+Ll_ISR_2006+Pl_ISR_2013+Ll_ISR_2013+Pl_ITA_2006+Ll_ITA_2006+Pl_ITA_2008+Ll_ITA_2008+Pl_ITA_2018+Ll_ITA_2018+Pl_NLD_1986+Ll_NLD_1986+Pl_NLD_1994+Ll_NLD_1994+Pl_NLD_1998+Ll_NLD_1998+Pl_NLD_2002+Ll_NLD_2002+Pl_NLD_2006+Ll_NLD_2006+Pl_NLD_2010+Ll_NLD_2010+Pl_NOR_1981+Ll_NOR_1981+Pl_NOR_1985+Ll_NOR_1985+Pl_NOR_1989+Ll_NOR_1989+Pl_NOR_1993+Ll_NOR_1993+Pl_NOR_1997+Ll_NOR_1997+Pl_NOR_2001+Ll_NOR_2001+Pl_NOR_2005+Ll_NOR_2005+Pl_NOR_2009+Ll_NOR_2009+Pl_NOR_2013+Ll_NOR_2013+Pl_NOR_2017+Ll_NOR_2017+Pl_NZL_1996+Ll_NZL_1996+Pl_NZL_2008+Ll_NZL_2008+Pl_NZL_2011+Ll_NZL_2011+Pl_NZL_2014+Ll_NZL_2014+Pl_NZL_2017+Ll_NZL_2017+Pl_PRT_2002+Ll_PRT_2002+Pl_PRT_2005+Ll_PRT_2005+Pl_PRT_2009+Ll_PRT_2009+Pl_PRT_2015+Ll_PRT_2015+Pl_SWE_1979+Ll_SWE_1979+Pl_SWE_1982+Ll_SWE_1982+Pl_SWE_1985+Ll_SWE_1985+Pl_SWE_1988+Ll_SWE_1988+Pl_SWE_1991+Ll_SWE_1991+Pl_SWE_1994+Ll_SWE_1994+Pl_SWE_1998+Ll_SWE_1998+Pl_SWE_2002+Ll_SWE_2002+Pl_SWE_2006+Ll_SWE_2006+Pl_SWE_2010+Ll_SWE_2010+Pl_SWE_2014+Ll_SWE_2014
                 +strata(Esalt), robust=T, data=de, weights=Rwc, method="efron")

#change names for stargazer:
names(CLC_tesp_$coefficients)[names(CLC_tesp_$coefficients)=="Pl:Mtesp"]="Party Likability * Media Use"
names(CLC_tesp_$coefficients)[names(CLC_tesp_$coefficients)=="Mtesp:Ll"]="Leader Likability * Media Use"


##Table 4##
stargazer(CLC_tesp_)


##MEDIA - RC MEASURING MTELE-MNWSP - ALTERNATIVE THRESHOLDS:----

##starting value for thresholds -4
#time variables:
t0=-4#starting value for thresholds (-4,-2,0,2,4)
de$time3=pmin(de$time,30+t0)
de$time4=pmax(30+t0,pmin(de$time,40+t0))
de$time5=pmax(40+t0,de$time)

#regressions all respondents:
LMm_m4=lm_robust(Mtesp ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
Lm_m4=lm(Mtesp ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression

#plots all respondents:
# tikz(paste0("plot_","LMm_m4",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Television $-$ Newspaper") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(-0.4999,0.5)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        # geom_histogram(aes(x=Ey,y=..density..),binwidth = 1,color="gray85",fill="gray85",data=de[!is.na(de$Mtesp),]) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMm_m4,-4)) )
# dev.off()


##starting value for thresholds -2
#time variables:
t0=-2#starting value for thresholds (-4,-2,0,2,4)
de$time3=pmin(de$time,30+t0)
de$time4=pmax(30+t0,pmin(de$time,40+t0))
de$time5=pmax(40+t0,de$time)

#regressions all respondents:
LMm_m2=lm_robust(Mtesp ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
Lm_m2=lm(Mtesp ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression

#plots all respondents:
# tikz(paste0("plot_","LMm_m2",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Television $-$ Newspaper") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(-0.4999,0.5)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        # geom_histogram(aes(x=Ey,y=..density..),binwidth = 1,color="gray85",fill="gray85",data=de[!is.na(de$Mtesp),]) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMm_m2,-2)) )
# dev.off()


##starting value for thresholds 2
#time variables:
t0=2#starting value for thresholds (-4,-2,0,2,4)
de$time3=pmin(de$time,30+t0)
de$time4=pmax(30+t0,pmin(de$time,40+t0))
de$time5=pmax(40+t0,de$time)

#regressions all respondents:
LMm_2=lm_robust(Mtesp ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
Lm_2=lm(Mtesp ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression

#plots all respondents:
# tikz(paste0("plot_","LMm_2",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Television $-$ Newspaper") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(-0.4999,0.5)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        # geom_histogram(aes(x=Ey,y=..density..),binwidth = 1,color="gray85",fill="gray85",data=de[!is.na(de$Mtesp),]) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMm_2,2)) )
# dev.off()


##starting value for thresholds 4
#time variables:
t0=4#starting value for thresholds (-4,-2,0,2,4)
de$time3=pmin(de$time,30+t0)
de$time4=pmax(30+t0,pmin(de$time,40+t0))
de$time5=pmax(40+t0,de$time)

#regressions all respondents:
LMm_4=lm_robust(Mtesp ~ time3+time4+time5+factor(Ec)-1, cluster=Ec, data=de[de$Ey>=1979,], se_type="stata")#regression
Lm_4=lm(Mtesp ~ time3+time4+time5+factor(Ec)-1, data=de[de$Ey>=1979,], se_type="stata")#regression

#plots all respondents:
# tikz(paste0("plot_","LMm_4",".tex"),width=4, height=3)#plot Ll:
plot( ggplot() 
      +ggtitle("Television $-$ Newspaper") 
      +xlab("Year") +ylab("Frequency Use") + coord_cartesian(ylim=c(-0.4999,0.5)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        # geom_histogram(aes(x=Ey,y=..density..),binwidth = 1,color="gray85",fill="gray85",data=de[!is.na(de$Mtesp),]) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.LMMediafun(LMm_4,4)) )
# dev.off()


##Table D8##
##table full results:
stargazer(Lm_m4,Lm_m2,L_Mtesp_0,Lm_2,Lm_4,
          se = list(LMm_m4$std.error,LMm_m2$std.error,LM_Mtesp_0$std.error,LMm_2$std.error,LMm_4$std.error))


###MEDIA - EFFECT OF MEDIA USE ON LIKABILITY:----

##PlV-PlnV:
dt1=de %>% filter(Va==F) %>% group_by(chid) %>% mutate(PlnV=mean(Pl,na.rm=T))#avg Pl non-choices
dt1=dt1 %>% group_by(chid) %>% filter(row_number() == 1)#keep only one obs per respondent
dt2=de %>% filter(Va==T) %>% group_by(chid) %>% mutate(PlV=mean(Pl,na.rm=T))#Pl choices
dt2=dt2[,c("PlV")]#keep only var of interest
dt3=de %>% filter(Va==F) %>% group_by(chid) %>% mutate(LlnV=mean(Ll,na.rm=T))#avg Ll non-choices
dt3=dt3 %>% group_by(chid) %>% filter(row_number() == 1)#keep only one obs per respondent
dt3=dt3[,c("LlnV")]#keep only var of interest
dt4=de %>% filter(Va==T) %>% group_by(chid) %>% mutate(LlV=mean(Ll,na.rm=T))#Ll choices
dt4=dt4[,c("LlV")]#keep only var of interest

dt=cbind(dt1,dt2,dt3,dt4)
rm(dt1,dt2,dt3,dt4)

dt$PlVnV=dt$PlV-dt$PlnV
dt$LlVnV=dt$LlV-dt$LlnV

#base for stargazer non robust regressions:
LM_PlVnVMnwsp=lm(PlVnV ~ Mnwsp, data=dt)
LM_PlVnVMradi=lm(PlVnV ~ Mradi, data=dt)
LM_PlVnVMtele=lm(PlVnV ~ Mtele, data=dt)
LM_PlVnVMinte=lm(PlVnV ~ Minte, data=dt)
LM_LlVnVMnwsp=lm(LlVnV ~ Mnwsp, data=dt)
LM_LlVnVMradi=lm(LlVnV ~ Mradi, data=dt)
LM_LlVnVMtele=lm(LlVnV ~ Mtele, data=dt)
LM_LlVnVMinte=lm(LlVnV ~ Minte, data=dt)
#robust regressions:
LMR_PlVnVMnwsp=lm_robust(PlVnV ~ Mnwsp, data=dt, cluster=chid, se_type="stata")
LMR_PlVnVMradi=lm_robust(PlVnV ~ Mradi, data=dt, cluster=chid, se_type="stata")
LMR_PlVnVMtele=lm_robust(PlVnV ~ Mtele, data=dt, cluster=chid, se_type="stata")
LMR_PlVnVMinte=lm_robust(PlVnV ~ Minte, data=dt, cluster=chid, se_type="stata")
LMR_LlVnVMnwsp=lm_robust(LlVnV ~ Mnwsp, data=dt, cluster=chid, se_type="stata")
LMR_LlVnVMradi=lm_robust(LlVnV ~ Mradi, data=dt, cluster=chid, se_type="stata")
LMR_LlVnVMtele=lm_robust(LlVnV ~ Mtele, data=dt, cluster=chid, se_type="stata")
LMR_LlVnVMinte=lm_robust(LlVnV ~ Minte, data=dt, cluster=chid, se_type="stata")
#change names for stargazer:
names(LM_PlVnVMnwsp$coefficients)[names(LM_PlVnVMnwsp$coefficients)=="Mnwsp"]="Media Use"
names(LM_PlVnVMradi$coefficients)[names(LM_PlVnVMradi$coefficients)=="Mradi"]="Media Use"
names(LM_PlVnVMtele$coefficients)[names(LM_PlVnVMtele$coefficients)=="Mtele"]="Media Use"
names(LM_PlVnVMinte$coefficients)[names(LM_PlVnVMinte$coefficients)=="Minte"]="Media Use"
names(LM_LlVnVMnwsp$coefficients)[names(LM_LlVnVMnwsp$coefficients)=="Mnwsp"]="Media Use"
names(LM_LlVnVMradi$coefficients)[names(LM_LlVnVMradi$coefficients)=="Mradi"]="Media Use"
names(LM_LlVnVMtele$coefficients)[names(LM_LlVnVMtele$coefficients)=="Mtele"]="Media Use"
names(LM_LlVnVMinte$coefficients)[names(LM_LlVnVMinte$coefficients)=="Minte"]="Media Use"
names(LMR_PlVnVMnwsp$std.error)[names(LMR_PlVnVMnwsp$std.error)=="Mnwsp"]="Media Use"
names(LMR_PlVnVMradi$std.error)[names(LMR_PlVnVMradi$std.error)=="Mradi"]="Media Use"
names(LMR_PlVnVMtele$std.error)[names(LMR_PlVnVMtele$std.error)=="Mtele"]="Media Use"
names(LMR_PlVnVMinte$std.error)[names(LMR_PlVnVMinte$std.error)=="Minte"]="Media Use"
names(LMR_LlVnVMnwsp$std.error)[names(LMR_LlVnVMnwsp$std.error)=="Mnwsp"]="Media Use"
names(LMR_LlVnVMradi$std.error)[names(LMR_LlVnVMradi$std.error)=="Mradi"]="Media Use"
names(LMR_LlVnVMtele$std.error)[names(LMR_LlVnVMtele$std.error)=="Mtele"]="Media Use"
names(LMR_LlVnVMinte$std.error)[names(LMR_LlVnVMinte$std.error)=="Minte"]="Media Use"


##Table 5##
#table:
stargazer(LM_PlVnVMnwsp,LM_PlVnVMradi,LM_PlVnVMtele,LM_PlVnVMinte,
          se = list(LMR_PlVnVMnwsp$std.error,LMR_PlVnVMradi$std.error,LMR_PlVnVMtele$std.error,LMR_PlVnVMinte$std.error) )
stargazer(LM_LlVnVMnwsp,LM_LlVnVMradi,LM_LlVnVMtele,LM_LlVnVMinte,
          se = list(LMR_LlVnVMnwsp$std.error,LMR_LlVnVMradi$std.error,LMR_LlVnVMtele$std.error,LMR_LlVnVMinte$std.error) )

#media PlV-PlnV impact on Pl-Ll:
CLC_base=clogit(Va ~ Pl+Ll+strata(chid), robust=T, data=de, method="efron")#base effect
CLC_base$coefficients["Pl"]-CLC_base$coefficients["Ll"]#0.689134#Pl-Ll base effect
dte=de
dte$Pl[dte$Va==T]=dte$Pl[dte$Va==T]+(CI.LMMediafun(LM_Mtele_0,0)[22,1]-CI.LMMediafun(LM_Mtele_0,0)[12,1])*LM_PlVnVMtele$coefficients[2]
dte$Ll[dte$Va==T]=dte$Ll[dte$Va==T]+(CI.LMMediafun(LM_Mtele_0,0)[22,1]-CI.LMMediafun(LM_Mtele_0,0)[12,1])*LM_LlVnVMtele$coefficients[2]
CLC_LMRVnVMtele=clogit(Va ~ Pl+Ll+strata(chid), robust=T, data=dte, method="efron")#w/ media effect
CLC_LMRVnVMtele$coefficients["Pl"]-CLC_LMRVnVMtele$coefficients["Ll"]#0.6865852#Pl-Ll w/ media effect
# (Pl-Ll w/ media effect) - (Pl-Ll base effect):
CLC_LMRVnVMtele$coefficients["Pl"]-CLC_LMRVnVMtele$coefficients["Ll"]-(CLC_base$coefficients["Pl"]-CLC_base$coefficients["Ll"])
#-0.002548789

#Z-test:
m=-0.037-(-0.0025)#residual drop = drop in 90s - drop due to Mtele effect on Pl,Ll
# (drop in 90s from Party/Leader Effect - 10 Years Segment Models)
se=0.011#standard error drop in 90s
z=m/se#Z score
p=2*pnorm(-abs(z))#p-value != (two tails)
p#0.001
#the 90s drop remains significant even factoring out the possible effect of Mtele on Pl,Ll


#RC WITH BIGGEST 3 PARTIES - READ DATA 3 PARTIES:----

#read data:
load("d29_NP3.RData")

#base for country-slope FE:
#Ec:Pl FE:
for (i1 in 1:length(sort(unique(de$Ec)))) {#creating dummy variables Ec:Pl (for FE)
  de[[paste0("Pl_",sort(unique(de$Ec))[i1])]]=ifelse(de$Ec==sort(unique(de$Ec))[i1],de$Pl,0)#de$Pl
}
#Ec:Ll FE:
for (i1 in 1:length(sort(unique(de$Ec)))) {#creating dummy variables Ec:Ll (for FE)
  de[[paste0("Ll_",sort(unique(de$Ec))[i1])]]=ifelse(de$Ec==sort(unique(de$Ec))[i1],de$Ll,0)#de$Ll
}

#base for density plots:
#create dataset density all data:
dyALL = de[!is.na(de$Va)&!is.na(de$Pl)&!is.na(de$Ll),] %>% group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dyALL$Eyn=dyALL$Eyn/1020288#density by Ey
dyALL=dyALL %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey
#create dataset density data with left-right self reported position:
dyLRR = de[!is.na(de$Va)&!is.na(de$Pl)&!is.na(de$Ll)&!is.na(de$LRR),] %>% group_by(Ey) %>% mutate(Eyn=n())#number of obs by Ey
dyLRR$Eyn=dyLRR$Eyn/1020288#density by Ey
dyLRR=dyLRR %>% group_by(Ey) %>% filter(row_number()==1)#keep only 1 obs per Ey


#RC WITH BIGGEST 3 PARTIES - 10Y SEGMENTS:----

#confidence interval functions:
CI.PlLllses10fun=function(CLC,t0,pe){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    # t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.PlLl[t,1]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6-
      (CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]-
      2*1*1*vcov(CLC)["Pl_ISL","Ll_ISL"]+2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}
CI.Pllses10fun=function(CLC,t0,pe){ #function creating Confidence Interval for Pl
  CI.Pl=matrix(NA,nrow=59,ncol=4)#Pl:
  for (t in 1:59) {
    # t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.Pl[t,1]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6#fitted values
    CI.Pl[t,2]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]+2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]+
      2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]+2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]+
      2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]+
      2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]#Variance
    CI.Pl[t,3]=CI.Pl[t,1]-qnorm(0.975)*sqrt(CI.Pl[t,2])#95% CI lower
    CI.Pl[t,4]=CI.Pl[t,1]+qnorm(0.975)*sqrt(CI.Pl[t,2])#95% CI upper
  }
  CI.Pl=as.data.frame(CI.Pl)
  CI.Pl$t=c(1961:2019)
  if (pe==1) {CI.Pl=CI.Pl[1:(10+t0),]}
  if (pe==2) {CI.Pl=CI.Pl[(11+t0):(20+t0),]}
  if (pe==3) {CI.Pl=CI.Pl[(21+t0):(30+t0),]}
  if (pe==4) {CI.Pl=CI.Pl[(31+t0):(40+t0),]}
  if (pe==5) {CI.Pl=CI.Pl[(41+t0):(50+t0),]}
  if (pe==6) {CI.Pl=CI.Pl[(51+t0):59,]}
  CI.Pl
}
CI.Lllses10fun=function(CLC,t0,pe){ #function creating Confidence Interval for Ll
  CI.Ll=matrix(NA,nrow=59,ncol=4)#Ll:
  for (t in 1:59) {
    # t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.Ll[t,1]=CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6#fitted values
    CI.Ll[t,2]=1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]+
      2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]+
      2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]+
      2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]+
      2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]#Variance
    CI.Ll[t,3]=CI.Ll[t,1]-qnorm(0.975)*sqrt(CI.Ll[t,2])#95% CI lower
    CI.Ll[t,4]=CI.Ll[t,1]+qnorm(0.975)*sqrt(CI.Ll[t,2])#95% CI upper
  }
  CI.Ll=as.data.frame(CI.Ll)
  CI.Ll$t=c(1961:2019)
  if (pe==1) {CI.Ll=CI.Ll[1:(10+t0),]}
  if (pe==2) {CI.Ll=CI.Ll[(11+t0):(20+t0),]}
  if (pe==3) {CI.Ll=CI.Ll[(21+t0):(30+t0),]}
  if (pe==4) {CI.Ll=CI.Ll[(31+t0):(40+t0),]}
  if (pe==5) {CI.Ll=CI.Ll[(41+t0):(50+t0),]}
  if (pe==6) {CI.Ll=CI.Ll[(51+t0):59,]}
  CI.Ll
}

#Z-test functions:
ZtestfunT12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunT=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regressions:
CLC_lses103_ea_0=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                          +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                        +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses103_ea_0$coefficients)[names(CLC_lses103_ea_0$coefficients)=="time2:Ll"]="Ll:time2"


##Table F1##
#table (hypothesis testing):
stargazer(CLC_lses103_ea_0,no.space = T,single.row = T)
ZtestfunT12(CLC_lses103_ea_0)
ZtestfunT(CLC_lses103_ea_0,2,3)
ZtestfunT(CLC_lses103_ea_0,3,4)
ZtestfunT(CLC_lses103_ea_0,4,5)
ZtestfunT(CLC_lses103_ea_0,5,6)
#plots (median cty):
# tikz(paste0("plot_","CLC_lses103","_PlLl_0.tex"),width=4, height=3)#plot Pl-Ll:
plot( ggplot() +ggtitle("")
      +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10fun(CLC_lses103_ea_0,0,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10fun(CLC_lses103_ea_0,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10fun(CLC_lses103_ea_0,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10fun(CLC_lses103_ea_0,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10fun(CLC_lses103_ea_0,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.PlLllses10fun(CLC_lses103_ea_0,0,6)) )
# dev.off()


##Figure F1##
##plots only Pl,Ll:
# tikz(paste0("plot_","CLC_lses103","_Pl_0.tex"),width=4, height=3)#plot Pl:
plot( ggplot()
      +ggtitle("")
      +xlab("Year") +ylab("Party Effect") + coord_cartesian(ylim=c(0.5001,0.7)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.7-0.5001)+0.5001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses103_ea_0,0,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses103_ea_0,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses103_ea_0,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses103_ea_0,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses103_ea_0,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Pllses10fun(CLC_lses103_ea_0,0,6)) )
# dev.off()
# tikz(paste0("plot_","CLC_lses103","_Ll_0.tex"),width=4, height=3)#plot Ll:
plot( ggplot()
      +ggtitle("")
      +xlab("Year") +ylab("Leader Effect") + coord_cartesian(ylim=c(0.0001,0.2)) + scale_y_continuous(expand=c(0,0)) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.2-0.0001)+0.0001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses103_ea_0,0,1)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses103_ea_0,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses103_ea_0,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses103_ea_0,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses103_ea_0,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", lwd=2, CI.Lllses10fun(CLC_lses103_ea_0,0,6)) )
# dev.off()


#RC WITH BIGGEST 3 PARTIES - 10Y SEGMENTS BY LRR:----

#left-right - 0.25 quantile:
de=de %>% group_by(Es) %>% mutate(LRR25 = quantile(LRR,0.25,na.rm=TRUE))#0.25 quantile by Es (left)
de$DL=ifelse(de$LRR<de$LRR25,1,0)# left # population 1/4,3/4
de$DR=ifelse(de$LRR>=de$LRR25,1,0)# right

#confidence interval functions:
CI.PlLllses10LRfun=function(CLC,t0,pe){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=8)#Pl:
  for (t in 1:59) {
    t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.PlLl[t,1]=CLC$coefficients["Pl_DEU"]+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6+
      CLC$coefficients["Pl:time2:DL"]*t2+CLC$coefficients["Pl:time3:DL"]*t3+CLC$coefficients["Pl:time4:DL"]*t4+CLC$coefficients["Pl:time5:DL"]*t5+CLC$coefficients["Pl:time6:DL"]*t6-
      (CLC$coefficients["Ll_DEU"]+CLC$coefficients["time3:Ll"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6+
         CLC$coefficients["Ll:time2:DL"]*t2+CLC$coefficients["time3:Ll:DL"]*t3+CLC$coefficients["Ll:time4:DL"]*t4+CLC$coefficients["Ll:time5:DL"]*t5+CLC$coefficients["Ll:time6:DL"]*t6)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)["Pl_DEU","Pl_DEU"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      t2^2*vcov(CLC)["Pl:time2:DL","Pl:time2:DL"]+t3^2*vcov(CLC)["Pl:time3:DL","Pl:time3:DL"]+t4^2*vcov(CLC)["Pl:time4:DL","Pl:time4:DL"]+t5^2*vcov(CLC)["Pl:time5:DL","Pl:time5:DL"]+t6^2*vcov(CLC)["Pl:time6:DL","Pl:time6:DL"]+
      1*vcov(CLC)["Ll_DEU","Ll_DEU"]+t3^2*vcov(CLC)["time3:Ll","time3:Ll"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      t2^2*vcov(CLC)["Ll:time2:DL","Ll:time2:DL"]+t3^2*vcov(CLC)["time3:Ll:DL","time3:Ll:DL"]+t4^2*vcov(CLC)["Ll:time4:DL","Ll:time4:DL"]+t5^2*vcov(CLC)["Ll:time5:DL","Ll:time5:DL"]+t6^2*vcov(CLC)["Ll:time6:DL","Ll:time6:DL"]-
      2*1*1*vcov(CLC)["Pl_DEU","Ll_DEU"]+2*1*t3*vcov(CLC)["Pl_DEU","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_DEU","time3:Ll"]+
      2*1*t4*vcov(CLC)["Pl_DEU","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_DEU","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_DEU","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_DEU","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_DEU","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_DEU","Ll:time6"]+
      2*1*t2*vcov(CLC)["Pl_DEU","Pl:time2:DL"]-2*1*t2*vcov(CLC)["Pl_DEU","Ll:time2:DL"]+2*1*t3*vcov(CLC)["Pl_DEU","Pl:time3:DL"]-2*1*t3*vcov(CLC)["Pl_DEU","time3:Ll:DL"]+
      2*1*t4*vcov(CLC)["Pl_DEU","Pl:time4:DL"]-2*1*t4*vcov(CLC)["Pl_DEU","Ll:time4:DL"]+2*1*t5*vcov(CLC)["Pl_DEU","Pl:time5:DL"]-2*1*t5*vcov(CLC)["Pl_DEU","Ll:time5:DL"]+2*1*t6*vcov(CLC)["Pl_DEU","Pl:time6:DL"]-2*1*t6*vcov(CLC)["Pl_DEU","Ll:time6:DL"]-
      2*1*t3*vcov(CLC)["Ll_DEU","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_DEU","time3:Ll"]-
      2*1*t4*vcov(CLC)["Ll_DEU","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_DEU","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_DEU","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_DEU","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_DEU","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_DEU","Ll:time6"]-
      2*1*t2*vcov(CLC)["Ll_DEU","Pl:time2:DL"]+2*1*t2*vcov(CLC)["Ll_DEU","Ll:time2:DL"]-2*1*t3*vcov(CLC)["Ll_DEU","Pl:time3:DL"]+2*1*t3*vcov(CLC)["Ll_DEU","time3:Ll:DL"]-
      2*1*t4*vcov(CLC)["Ll_DEU","Pl:time4:DL"]+2*1*t4*vcov(CLC)["Ll_DEU","Ll:time4:DL"]-2*1*t5*vcov(CLC)["Ll_DEU","Pl:time5:DL"]+2*1*t5*vcov(CLC)["Ll_DEU","Ll:time5:DL"]-2*1*t6*vcov(CLC)["Ll_DEU","Pl:time6:DL"]+2*1*t6*vcov(CLC)["Ll_DEU","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3","time3:Ll"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]+
      2*t3*t2*vcov(CLC)["Pl:time3","Pl:time2:DL"]-2*t3*t2*vcov(CLC)["Pl:time3","Ll:time2:DL"]+2*t3*t3*vcov(CLC)["Pl:time3","Pl:time3:DL"]-2*t3*t3*vcov(CLC)["Pl:time3","time3:Ll:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["time3:Ll","Pl:time4"]+2*t3*t4*vcov(CLC)["time3:Ll","Ll:time4"]-2*t3*t5*vcov(CLC)["time3:Ll","Pl:time5"]+2*t3*t5*vcov(CLC)["time3:Ll","Ll:time5"]-2*t3*t6*vcov(CLC)["time3:Ll","Pl:time6"]+2*t3*t6*vcov(CLC)["time3:Ll","Ll:time6"]-
      2*t3*t2*vcov(CLC)["time3:Ll","Pl:time2:DL"]+2*t3*t2*vcov(CLC)["time3:Ll","Ll:time2:DL"]-2*t3*t3*vcov(CLC)["time3:Ll","Pl:time3:DL"]+2*t3*t3*vcov(CLC)["time3:Ll","time3:Ll:DL"]-
      2*t3*t4*vcov(CLC)["time3:Ll","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["time3:Ll","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["time3:Ll","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["time3:Ll","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["time3:Ll","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["time3:Ll","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]+
      2*t4*t2*vcov(CLC)["Pl:time4","Pl:time2:DL"]-2*t4*t2*vcov(CLC)["Pl:time4","Ll:time2:DL"]+2*t4*t3*vcov(CLC)["Pl:time4","Pl:time3:DL"]-2*t4*t3*vcov(CLC)["Pl:time4","time3:Ll:DL"]+
      2*t4*t4*vcov(CLC)["Pl:time4","Pl:time4:DL"]-2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t4*t2*vcov(CLC)["Ll:time4","Pl:time2:DL"]+2*t4*t2*vcov(CLC)["Ll:time4","Ll:time2:DL"]-2*t4*t3*vcov(CLC)["Ll:time4","Pl:time3:DL"]+2*t4*t3*vcov(CLC)["Ll:time4","time3:Ll:DL"]-
      2*t4*t4*vcov(CLC)["Ll:time4","Pl:time4:DL"]+2*t4*t4*vcov(CLC)["Ll:time4","Ll:time4:DL"]-2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]+
      2*t5*t2*vcov(CLC)["Pl:time5","Pl:time2:DL"]-2*t5*t2*vcov(CLC)["Pl:time5","Ll:time2:DL"]+2*t5*t3*vcov(CLC)["Pl:time5","Pl:time3:DL"]-2*t5*t3*vcov(CLC)["Pl:time5","time3:Ll:DL"]+
      2*t5*t4*vcov(CLC)["Pl:time5","Pl:time4:DL"]-2*t5*t4*vcov(CLC)["Pl:time5","Ll:time4:DL"]+2*t5*t5*vcov(CLC)["Pl:time5","Pl:time5:DL"]-2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t5*t2*vcov(CLC)["Ll:time5","Pl:time2:DL"]+2*t5*t2*vcov(CLC)["Ll:time5","Ll:time2:DL"]-2*t5*t3*vcov(CLC)["Ll:time5","Pl:time3:DL"]+2*t5*t3*vcov(CLC)["Ll:time5","time3:Ll:DL"]-
      2*t5*t4*vcov(CLC)["Ll:time5","Pl:time4:DL"]+2*t5*t4*vcov(CLC)["Ll:time5","Ll:time4:DL"]-2*t5*t5*vcov(CLC)["Ll:time5","Pl:time5:DL"]+2*t5*t5*vcov(CLC)["Ll:time5","Ll:time5:DL"]-2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]+
      2*t6*t2*vcov(CLC)["Pl:time6","Pl:time2:DL"]-2*t6*t2*vcov(CLC)["Pl:time6","Ll:time2:DL"]+2*t6*t3*vcov(CLC)["Pl:time6","Pl:time3:DL"]-2*t6*t3*vcov(CLC)["Pl:time6","time3:Ll:DL"]+
      2*t6*t4*vcov(CLC)["Pl:time6","Pl:time4:DL"]-2*t6*t4*vcov(CLC)["Pl:time6","Ll:time4:DL"]+2*t6*t5*vcov(CLC)["Pl:time6","Pl:time5:DL"]-2*t6*t5*vcov(CLC)["Pl:time6","Ll:time5:DL"]+2*t6*t6*vcov(CLC)["Pl:time6","Pl:time6:DL"]-2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6:DL"]-
      2*t6*t2*vcov(CLC)["Ll:time6","Pl:time2:DL"]+2*t6*t2*vcov(CLC)["Ll:time6","Ll:time2:DL"]-2*t6*t3*vcov(CLC)["Ll:time6","Pl:time3:DL"]+2*t6*t3*vcov(CLC)["Ll:time6","time3:Ll:DL"]-
      2*t6*t4*vcov(CLC)["Ll:time6","Pl:time4:DL"]+2*t6*t4*vcov(CLC)["Ll:time6","Ll:time4:DL"]-2*t6*t5*vcov(CLC)["Ll:time6","Pl:time5:DL"]+2*t6*t5*vcov(CLC)["Ll:time6","Ll:time5:DL"]-2*t6*t6*vcov(CLC)["Ll:time6","Pl:time6:DL"]+2*t6*t6*vcov(CLC)["Ll:time6","Ll:time6:DL"]-
      2*t2*t2*vcov(CLC)["Pl:time2:DL","Ll:time2:DL"]+2*t2*t3*vcov(CLC)["Pl:time2:DL","Pl:time3:DL"]-2*t2*t3*vcov(CLC)["Pl:time2:DL","time3:Ll:DL"]+
      2*t2*t4*vcov(CLC)["Pl:time2:DL","Pl:time4:DL"]-2*t2*t4*vcov(CLC)["Pl:time2:DL","Ll:time4:DL"]+2*t2*t5*vcov(CLC)["Pl:time2:DL","Pl:time5:DL"]-2*t2*t5*vcov(CLC)["Pl:time2:DL","Ll:time5:DL"]+2*t2*t6*vcov(CLC)["Pl:time2:DL","Pl:time6:DL"]-2*t2*t6*vcov(CLC)["Pl:time2:DL","Ll:time6:DL"]-
      2*t2*t3*vcov(CLC)["Ll:time2:DL","Pl:time3:DL"]+2*t2*t3*vcov(CLC)["Ll:time2:DL","time3:Ll:DL"]-
      2*t2*t4*vcov(CLC)["Ll:time2:DL","Pl:time4:DL"]+2*t2*t4*vcov(CLC)["Ll:time2:DL","Ll:time4:DL"]-2*t2*t5*vcov(CLC)["Ll:time2:DL","Pl:time5:DL"]+2*t2*t5*vcov(CLC)["Ll:time2:DL","Ll:time5:DL"]-2*t2*t6*vcov(CLC)["Ll:time2:DL","Pl:time6:DL"]+2*t2*t6*vcov(CLC)["Ll:time2:DL","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3:DL","time3:Ll:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3:DL","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3:DL","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3:DL","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3:DL","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3:DL","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3:DL","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["time3:Ll:DL","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["time3:Ll:DL","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["time3:Ll:DL","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["time3:Ll:DL","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["time3:Ll:DL","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["time3:Ll:DL","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4:DL","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4:DL","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4:DL","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4:DL","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4:DL","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4:DL","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4:DL","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4:DL","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4:DL","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5:DL","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5:DL","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5:DL","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5:DL","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5:DL","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6:DL","Ll:time6:DL"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
    #right:
    CI.PlLl[t,5]=CLC$coefficients["Pl_DEU"]+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6-
      (CLC$coefficients["Ll_DEU"]+CLC$coefficients["time3:Ll"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6)#fitted values
    CI.PlLl[t,6]=1*vcov(CLC)["Pl_DEU","Pl_DEU"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      1*vcov(CLC)["Ll_DEU","Ll_DEU"]+t3^2*vcov(CLC)["time3:Ll","time3:Ll"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]-
      2*1*1*vcov(CLC)["Pl_DEU","Ll_DEU"]+2*1*t3*vcov(CLC)["Pl_DEU","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_DEU","time3:Ll"]+
      2*1*t4*vcov(CLC)["Pl_DEU","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_DEU","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_DEU","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_DEU","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_DEU","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_DEU","Ll:time6"]-
      2*1*t3*vcov(CLC)["Ll_DEU","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_DEU","time3:Ll"]-
      2*1*t4*vcov(CLC)["Ll_DEU","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_DEU","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_DEU","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_DEU","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_DEU","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_DEU","Ll:time6"]-
      2*t3*t3*vcov(CLC)["Pl:time3","time3:Ll"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]-
      2*t3*t4*vcov(CLC)["time3:Ll","Pl:time4"]+2*t3*t4*vcov(CLC)["time3:Ll","Ll:time4"]-2*t3*t5*vcov(CLC)["time3:Ll","Pl:time5"]+2*t3*t5*vcov(CLC)["time3:Ll","Ll:time5"]-2*t3*t6*vcov(CLC)["time3:Ll","Pl:time6"]+2*t3*t6*vcov(CLC)["time3:Ll","Ll:time6"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]
    CI.PlLl[t,7]=CI.PlLl[t,5]-qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI lower
    CI.PlLl[t,8]=CI.PlLl[t,5]+qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}
CI.PlLllses10LRfunCTY=function(CLC,t0,pe,cty){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=8)#Pl:
  for (t in 1:59) {
    t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    CI.PlLl[t,1]=CLC$coefficients[paste0("Pl_",cty)]+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6+
      CLC$coefficients["Pl:time2:DL"]*t2+CLC$coefficients["Pl:time3:DL"]*t3+CLC$coefficients["Pl:time4:DL"]*t4+CLC$coefficients["Pl:time5:DL"]*t5+CLC$coefficients["Pl:time6:DL"]*t6-
      (CLC$coefficients[paste0("Ll_",cty)]+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6+
         CLC$coefficients["Ll:time2:DL"]*t2+CLC$coefficients["Ll:time3:DL"]*t3+CLC$coefficients["Ll:time4:DL"]*t4+CLC$coefficients["Ll:time5:DL"]*t5+CLC$coefficients["Ll:time6:DL"]*t6)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)[paste0("Pl_",cty),paste0("Pl_",cty)]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      t2^2*vcov(CLC)["Pl:time2:DL","Pl:time2:DL"]+t3^2*vcov(CLC)["Pl:time3:DL","Pl:time3:DL"]+t4^2*vcov(CLC)["Pl:time4:DL","Pl:time4:DL"]+t5^2*vcov(CLC)["Pl:time5:DL","Pl:time5:DL"]+t6^2*vcov(CLC)["Pl:time6:DL","Pl:time6:DL"]+
      1*vcov(CLC)[paste0("Ll_",cty),paste0("Ll_",cty)]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      t2^2*vcov(CLC)["Ll:time2:DL","Ll:time2:DL"]+t3^2*vcov(CLC)["Ll:time3:DL","Ll:time3:DL"]+t4^2*vcov(CLC)["Ll:time4:DL","Ll:time4:DL"]+t5^2*vcov(CLC)["Ll:time5:DL","Ll:time5:DL"]+t6^2*vcov(CLC)["Ll:time6:DL","Ll:time6:DL"]-
      2*1*1*vcov(CLC)[paste0("Pl_",cty),paste0("Ll_",cty)]+2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Pl:time3"]-2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Ll:time3"]+
      2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Pl:time4"]-2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Ll:time4"]+2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Pl:time5"]-2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Ll:time5"]+2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Pl:time6"]-2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Ll:time6"]+
      2*1*t2*vcov(CLC)[paste0("Pl_",cty),"Pl:time2:DL"]-2*1*t2*vcov(CLC)[paste0("Pl_",cty),"Ll:time2:DL"]+2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Pl:time3:DL"]-2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Ll:time3:DL"]+
      2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Pl:time4:DL"]-2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Ll:time4:DL"]+2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Pl:time5:DL"]-2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Ll:time5:DL"]+2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Pl:time6:DL"]-2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Ll:time6:DL"]-
      2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Pl:time3"]+2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Ll:time3"]-
      2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Pl:time4"]+2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Ll:time4"]-2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Pl:time5"]+2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Ll:time5"]-2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Pl:time6"]+2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Ll:time6"]-
      2*1*t2*vcov(CLC)[paste0("Ll_",cty),"Pl:time2:DL"]+2*1*t2*vcov(CLC)[paste0("Ll_",cty),"Ll:time2:DL"]-2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Pl:time3:DL"]+2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Ll:time3:DL"]-
      2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Pl:time4:DL"]+2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Ll:time4:DL"]-2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Pl:time5:DL"]+2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Ll:time5:DL"]-2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Pl:time6:DL"]+2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]+
      2*t3*t2*vcov(CLC)["Pl:time3","Pl:time2:DL"]-2*t3*t2*vcov(CLC)["Pl:time3","Ll:time2:DL"]+2*t3*t3*vcov(CLC)["Pl:time3","Pl:time3:DL"]-2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t3*t2*vcov(CLC)["Ll:time3","Pl:time2:DL"]+2*t3*t2*vcov(CLC)["Ll:time3","Ll:time2:DL"]-2*t3*t3*vcov(CLC)["Ll:time3","Pl:time3:DL"]+2*t3*t3*vcov(CLC)["Ll:time3","Ll:time3:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]+
      2*t4*t2*vcov(CLC)["Pl:time4","Pl:time2:DL"]-2*t4*t2*vcov(CLC)["Pl:time4","Ll:time2:DL"]+2*t4*t3*vcov(CLC)["Pl:time4","Pl:time3:DL"]-2*t4*t3*vcov(CLC)["Pl:time4","Ll:time3:DL"]+
      2*t4*t4*vcov(CLC)["Pl:time4","Pl:time4:DL"]-2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t4*t2*vcov(CLC)["Ll:time4","Pl:time2:DL"]+2*t4*t2*vcov(CLC)["Ll:time4","Ll:time2:DL"]-2*t4*t3*vcov(CLC)["Ll:time4","Pl:time3:DL"]+2*t4*t3*vcov(CLC)["Ll:time4","Ll:time3:DL"]-
      2*t4*t4*vcov(CLC)["Ll:time4","Pl:time4:DL"]+2*t4*t4*vcov(CLC)["Ll:time4","Ll:time4:DL"]-2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]+
      2*t5*t2*vcov(CLC)["Pl:time5","Pl:time2:DL"]-2*t5*t2*vcov(CLC)["Pl:time5","Ll:time2:DL"]+2*t5*t3*vcov(CLC)["Pl:time5","Pl:time3:DL"]-2*t5*t3*vcov(CLC)["Pl:time5","Ll:time3:DL"]+
      2*t5*t4*vcov(CLC)["Pl:time5","Pl:time4:DL"]-2*t5*t4*vcov(CLC)["Pl:time5","Ll:time4:DL"]+2*t5*t5*vcov(CLC)["Pl:time5","Pl:time5:DL"]-2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t5*t2*vcov(CLC)["Ll:time5","Pl:time2:DL"]+2*t5*t2*vcov(CLC)["Ll:time5","Ll:time2:DL"]-2*t5*t3*vcov(CLC)["Ll:time5","Pl:time3:DL"]+2*t5*t3*vcov(CLC)["Ll:time5","Ll:time3:DL"]-
      2*t5*t4*vcov(CLC)["Ll:time5","Pl:time4:DL"]+2*t5*t4*vcov(CLC)["Ll:time5","Ll:time4:DL"]-2*t5*t5*vcov(CLC)["Ll:time5","Pl:time5:DL"]+2*t5*t5*vcov(CLC)["Ll:time5","Ll:time5:DL"]-2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]+
      2*t6*t2*vcov(CLC)["Pl:time6","Pl:time2:DL"]-2*t6*t2*vcov(CLC)["Pl:time6","Ll:time2:DL"]+2*t6*t3*vcov(CLC)["Pl:time6","Pl:time3:DL"]-2*t6*t3*vcov(CLC)["Pl:time6","Ll:time3:DL"]+
      2*t6*t4*vcov(CLC)["Pl:time6","Pl:time4:DL"]-2*t6*t4*vcov(CLC)["Pl:time6","Ll:time4:DL"]+2*t6*t5*vcov(CLC)["Pl:time6","Pl:time5:DL"]-2*t6*t5*vcov(CLC)["Pl:time6","Ll:time5:DL"]+2*t6*t6*vcov(CLC)["Pl:time6","Pl:time6:DL"]-2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6:DL"]-
      2*t6*t2*vcov(CLC)["Ll:time6","Pl:time2:DL"]+2*t6*t2*vcov(CLC)["Ll:time6","Ll:time2:DL"]-2*t6*t3*vcov(CLC)["Ll:time6","Pl:time3:DL"]+2*t6*t3*vcov(CLC)["Ll:time6","Ll:time3:DL"]-
      2*t6*t4*vcov(CLC)["Ll:time6","Pl:time4:DL"]+2*t6*t4*vcov(CLC)["Ll:time6","Ll:time4:DL"]-2*t6*t5*vcov(CLC)["Ll:time6","Pl:time5:DL"]+2*t6*t5*vcov(CLC)["Ll:time6","Ll:time5:DL"]-2*t6*t6*vcov(CLC)["Ll:time6","Pl:time6:DL"]+2*t6*t6*vcov(CLC)["Ll:time6","Ll:time6:DL"]-
      2*t2*t2*vcov(CLC)["Pl:time2:DL","Ll:time2:DL"]+2*t2*t3*vcov(CLC)["Pl:time2:DL","Pl:time3:DL"]-2*t2*t3*vcov(CLC)["Pl:time2:DL","Ll:time3:DL"]+
      2*t2*t4*vcov(CLC)["Pl:time2:DL","Pl:time4:DL"]-2*t2*t4*vcov(CLC)["Pl:time2:DL","Ll:time4:DL"]+2*t2*t5*vcov(CLC)["Pl:time2:DL","Pl:time5:DL"]-2*t2*t5*vcov(CLC)["Pl:time2:DL","Ll:time5:DL"]+2*t2*t6*vcov(CLC)["Pl:time2:DL","Pl:time6:DL"]-2*t2*t6*vcov(CLC)["Pl:time2:DL","Ll:time6:DL"]-
      2*t2*t3*vcov(CLC)["Ll:time2:DL","Pl:time3:DL"]+2*t2*t3*vcov(CLC)["Ll:time2:DL","Ll:time3:DL"]-
      2*t2*t4*vcov(CLC)["Ll:time2:DL","Pl:time4:DL"]+2*t2*t4*vcov(CLC)["Ll:time2:DL","Ll:time4:DL"]-2*t2*t5*vcov(CLC)["Ll:time2:DL","Pl:time5:DL"]+2*t2*t5*vcov(CLC)["Ll:time2:DL","Ll:time5:DL"]-2*t2*t6*vcov(CLC)["Ll:time2:DL","Pl:time6:DL"]+2*t2*t6*vcov(CLC)["Ll:time2:DL","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3:DL","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3:DL","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3:DL","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3:DL","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3:DL","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3:DL","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3:DL","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3:DL","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3:DL","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3:DL","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3:DL","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3:DL","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3:DL","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4:DL","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4:DL","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4:DL","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4:DL","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4:DL","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4:DL","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4:DL","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4:DL","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4:DL","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5:DL","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5:DL","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5:DL","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5:DL","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5:DL","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6:DL","Ll:time6:DL"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
    #right:
    CI.PlLl[t,5]=CLC$coefficients[paste0("Pl_",cty)]+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6-
      (CLC$coefficients[paste0("Ll_",cty)]+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6)#fitted values
    CI.PlLl[t,6]=1*vcov(CLC)[paste0("Pl_",cty),paste0("Pl_",cty)]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      1*vcov(CLC)[paste0("Ll_",cty),paste0("Ll_",cty)]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]-
      2*1*1*vcov(CLC)[paste0("Pl_",cty),paste0("Ll_",cty)]+2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Pl:time3"]-2*1*t3*vcov(CLC)[paste0("Pl_",cty),"Ll:time3"]+
      2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Pl:time4"]-2*1*t4*vcov(CLC)[paste0("Pl_",cty),"Ll:time4"]+2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Pl:time5"]-2*1*t5*vcov(CLC)[paste0("Pl_",cty),"Ll:time5"]+2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Pl:time6"]-2*1*t6*vcov(CLC)[paste0("Pl_",cty),"Ll:time6"]-
      2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Pl:time3"]+2*1*t3*vcov(CLC)[paste0("Ll_",cty),"Ll:time3"]-
      2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Pl:time4"]+2*1*t4*vcov(CLC)[paste0("Ll_",cty),"Ll:time4"]-2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Pl:time5"]+2*1*t5*vcov(CLC)[paste0("Ll_",cty),"Ll:time5"]-2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Pl:time6"]+2*1*t6*vcov(CLC)[paste0("Ll_",cty),"Ll:time6"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]
    CI.PlLl[t,7]=CI.PlLl[t,5]-qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI lower
    CI.PlLl[t,8]=CI.PlLl[t,5]+qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}

#Z-test functions:
ZtestfunT12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTD12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2,":DL")]-CLC$coefficients[paste0("Ll:time",2,":DL")]#mean
  v=vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Pl:time",2,":DL")]+vcov(CLC)[paste0("Ll:time",2,":DL"),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Ll:time",2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunD12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]+
    CLC$coefficients[paste0("Pl:time",2,":DL")]-CLC$coefficients[paste0("Ll:time",2,":DL")]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]+
    vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Pl:time",2,":DL")]+vcov(CLC)[paste0("Ll:time",2,":DL"),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]+2*vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",2),paste0("Pl:time",2,":DL")]+2*vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Ll:time",2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunT=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTD=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1,":DL")]-CLC$coefficients[paste0("Ll:time",per1,":DL")]-
        CLC$coefficients[paste0("Pl:time",per2,":DL")]+CLC$coefficients[paste0("Ll:time",per2,":DL")])#mean
  v=vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per1,":DL")]+vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per1,":DL")]+
    vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Pl:time",per2,":DL")]+vcov(CLC)[paste0("Ll:time",per2,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2,":DL")]-2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Ll:time",per2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunD=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]+
        CLC$coefficients[paste0("Pl:time",per1,":DL")]-CLC$coefficients[paste0("Ll:time",per1,":DL")]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)]-
        CLC$coefficients[paste0("Pl:time",per2,":DL")]+CLC$coefficients[paste0("Ll:time",per2,":DL")])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per1,":DL")]+vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per1,":DL")]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]+
    vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Pl:time",per2,":DL")]+vcov(CLC)[paste0("Ll:time",per2,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]+2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per1,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1,":DL")]+2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2)]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2)]-
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per2),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Ll:time",per2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regressions:
CLC_lses103_LR25_0=clogit(Va ~ Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                            Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                            +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                          +strata(Esalt), robust=T, data=de[!is.na(de$LRR),], method="efron")#regression
names(CLC_lses103_LR25_0$coefficients)[names(CLC_lses103_LR25_0$coefficients)=="time3:Ll"]="Ll:time3"
names(CLC_lses103_LR25_0$coefficients)[names(CLC_lses103_LR25_0$coefficients)=="time3:Ll:DL"]="Ll:time3:DL"


##Table F2##
#hypothesis testing:
stargazer(CLC_lses103_LR25_0,no.space = T,single.row = T)
#drop remaining voters:
ZtestfunT(CLC_lses103_LR25_0,3,4)
ZtestfunT(CLC_lses103_LR25_0,4,5)
ZtestfunT(CLC_lses103_LR25_0,5,6)
#drop left-quartile voters:
ZtestfunD(CLC_lses103_LR25_0,3,4)
ZtestfunD(CLC_lses103_LR25_0,4,5)
ZtestfunD(CLC_lses103_LR25_0,5,6)
#difference in drop between left-quartile and remaining voters:
ZtestfunTD(CLC_lses103_LR25_0,3,4)
ZtestfunTD(CLC_lses103_LR25_0,4,5)
ZtestfunTD(CLC_lses103_LR25_0,5,6)
#plots (median cty):
# tikz(paste0("plot_","CLC_lses103_LR25_0","_PlLl.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.4001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.4001)+0.4001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyLRR) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses103_LR25_0,0,3,sort(unique(de$Ec))[11])) +#left
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses103_LR25_0,0,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses103_LR25_0,0,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10LRfunCTY(CLC_lses103_LR25_0,0,6,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses103_LR25_0,0,3,sort(unique(de$Ec))[11])) +#right
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses103_LR25_0,0,4,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses103_LR25_0,0,5,sort(unique(de$Ec))[11])) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10LRfunCTY(CLC_lses103_LR25_0,0,6,sort(unique(de$Ec))[11])) )
# dev.off()


#RC WITH BIGGEST 3 PARTIES - 10Y SEGMENTS BY COUNTRY GERMANY AND TABLE:----

#Germany dummy:
de$DL[de$Ec %in% c("DEU")]=1#(high)
de$DL[!de$Ec %in% c("DEU")]=0#(low)

#confidence interval functions:
CI.PlLllses10CGfun=function(CLC,t0,pe){ #function creating Confidence Interval for Pl-Ll
  CI.PlLl=matrix(NA,nrow=59,ncol=8)#Pl:
  for (t in 1:59) {
    t1=ifelse(t<=10+t0,1,0)
    t2=ifelse(t>10+t0&t<=20+t0,1,0)
    t3=ifelse(t>20+t0&t<=30+t0,1,0)
    t4=ifelse(t>30+t0&t<=40+t0,1,0)
    t5=ifelse(t>40+t0&t<=50+t0,1,0)
    t6=ifelse(t>50+t0,1,0)
    #left:
    CI.PlLl[t,1]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6+
      CLC$coefficients["Pl:time2:DL"]*t2+CLC$coefficients["Pl:time3:DL"]*t3+CLC$coefficients["Pl:time4:DL"]*t4+CLC$coefficients["Pl:time5:DL"]*t5+CLC$coefficients["Pl:time6:DL"]*t6-
      (CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6+
         CLC$coefficients["Ll:time2:DL"]*t2+CLC$coefficients["Ll:time3:DL"]*t3+CLC$coefficients["Ll:time4:DL"]*t4+CLC$coefficients["Ll:time5:DL"]*t5+CLC$coefficients["Ll:time6:DL"]*t6)#fitted values
    CI.PlLl[t,2]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      t2^2*vcov(CLC)["Pl:time2:DL","Pl:time2:DL"]+t3^2*vcov(CLC)["Pl:time3:DL","Pl:time3:DL"]+t4^2*vcov(CLC)["Pl:time4:DL","Pl:time4:DL"]+t5^2*vcov(CLC)["Pl:time5:DL","Pl:time5:DL"]+t6^2*vcov(CLC)["Pl:time6:DL","Pl:time6:DL"]+
      1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]+
      t2^2*vcov(CLC)["Ll:time2:DL","Ll:time2:DL"]+t3^2*vcov(CLC)["Ll:time3:DL","Ll:time3:DL"]+t4^2*vcov(CLC)["Ll:time4:DL","Ll:time4:DL"]+t5^2*vcov(CLC)["Ll:time5:DL","Ll:time5:DL"]+t6^2*vcov(CLC)["Ll:time6:DL","Ll:time6:DL"]-
      2*1*1*vcov(CLC)["Pl_ISL","Ll_ISL"]+2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6"]+
      2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2:DL"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2:DL"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3:DL"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3:DL"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4:DL"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4:DL"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5:DL"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5:DL"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6:DL"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6:DL"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2:DL"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2:DL"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3:DL"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3:DL"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4:DL"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4:DL"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5:DL"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5:DL"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6:DL"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6:DL"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]+
      2*t2*t2*vcov(CLC)["Pl:time2","Pl:time2:DL"]-2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2:DL"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3:DL"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3:DL"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4:DL"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4:DL"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5:DL"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5:DL"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6:DL"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6:DL"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t2*t2*vcov(CLC)["Ll:time2","Pl:time2:DL"]+2*t2*t2*vcov(CLC)["Ll:time2","Ll:time2:DL"]-2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3:DL"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3:DL"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4:DL"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4:DL"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5:DL"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5:DL"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6:DL"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]+
      2*t3*t2*vcov(CLC)["Pl:time3","Pl:time2:DL"]-2*t3*t2*vcov(CLC)["Pl:time3","Ll:time2:DL"]+2*t3*t3*vcov(CLC)["Pl:time3","Pl:time3:DL"]-2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t3*t2*vcov(CLC)["Ll:time3","Pl:time2:DL"]+2*t3*t2*vcov(CLC)["Ll:time3","Ll:time2:DL"]-2*t3*t3*vcov(CLC)["Ll:time3","Pl:time3:DL"]+2*t3*t3*vcov(CLC)["Ll:time3","Ll:time3:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]+
      2*t4*t2*vcov(CLC)["Pl:time4","Pl:time2:DL"]-2*t4*t2*vcov(CLC)["Pl:time4","Ll:time2:DL"]+2*t4*t3*vcov(CLC)["Pl:time4","Pl:time3:DL"]-2*t4*t3*vcov(CLC)["Pl:time4","Ll:time3:DL"]+
      2*t4*t4*vcov(CLC)["Pl:time4","Pl:time4:DL"]-2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t4*t2*vcov(CLC)["Ll:time4","Pl:time2:DL"]+2*t4*t2*vcov(CLC)["Ll:time4","Ll:time2:DL"]-2*t4*t3*vcov(CLC)["Ll:time4","Pl:time3:DL"]+2*t4*t3*vcov(CLC)["Ll:time4","Ll:time3:DL"]-
      2*t4*t4*vcov(CLC)["Ll:time4","Pl:time4:DL"]+2*t4*t4*vcov(CLC)["Ll:time4","Ll:time4:DL"]-2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]+
      2*t5*t2*vcov(CLC)["Pl:time5","Pl:time2:DL"]-2*t5*t2*vcov(CLC)["Pl:time5","Ll:time2:DL"]+2*t5*t3*vcov(CLC)["Pl:time5","Pl:time3:DL"]-2*t5*t3*vcov(CLC)["Pl:time5","Ll:time3:DL"]+
      2*t5*t4*vcov(CLC)["Pl:time5","Pl:time4:DL"]-2*t5*t4*vcov(CLC)["Pl:time5","Ll:time4:DL"]+2*t5*t5*vcov(CLC)["Pl:time5","Pl:time5:DL"]-2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t5*t2*vcov(CLC)["Ll:time5","Pl:time2:DL"]+2*t5*t2*vcov(CLC)["Ll:time5","Ll:time2:DL"]-2*t5*t3*vcov(CLC)["Ll:time5","Pl:time3:DL"]+2*t5*t3*vcov(CLC)["Ll:time5","Ll:time3:DL"]-
      2*t5*t4*vcov(CLC)["Ll:time5","Pl:time4:DL"]+2*t5*t4*vcov(CLC)["Ll:time5","Ll:time4:DL"]-2*t5*t5*vcov(CLC)["Ll:time5","Pl:time5:DL"]+2*t5*t5*vcov(CLC)["Ll:time5","Ll:time5:DL"]-2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]+
      2*t6*t2*vcov(CLC)["Pl:time6","Pl:time2:DL"]-2*t6*t2*vcov(CLC)["Pl:time6","Ll:time2:DL"]+2*t6*t3*vcov(CLC)["Pl:time6","Pl:time3:DL"]-2*t6*t3*vcov(CLC)["Pl:time6","Ll:time3:DL"]+
      2*t6*t4*vcov(CLC)["Pl:time6","Pl:time4:DL"]-2*t6*t4*vcov(CLC)["Pl:time6","Ll:time4:DL"]+2*t6*t5*vcov(CLC)["Pl:time6","Pl:time5:DL"]-2*t6*t5*vcov(CLC)["Pl:time6","Ll:time5:DL"]+2*t6*t6*vcov(CLC)["Pl:time6","Pl:time6:DL"]-2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6:DL"]-
      2*t6*t2*vcov(CLC)["Ll:time6","Pl:time2:DL"]+2*t6*t2*vcov(CLC)["Ll:time6","Ll:time2:DL"]-2*t6*t3*vcov(CLC)["Ll:time6","Pl:time3:DL"]+2*t6*t3*vcov(CLC)["Ll:time6","Ll:time3:DL"]-
      2*t6*t4*vcov(CLC)["Ll:time6","Pl:time4:DL"]+2*t6*t4*vcov(CLC)["Ll:time6","Ll:time4:DL"]-2*t6*t5*vcov(CLC)["Ll:time6","Pl:time5:DL"]+2*t6*t5*vcov(CLC)["Ll:time6","Ll:time5:DL"]-2*t6*t6*vcov(CLC)["Ll:time6","Pl:time6:DL"]+2*t6*t6*vcov(CLC)["Ll:time6","Ll:time6:DL"]-
      2*t2*t2*vcov(CLC)["Pl:time2:DL","Ll:time2:DL"]+2*t2*t3*vcov(CLC)["Pl:time2:DL","Pl:time3:DL"]-2*t2*t3*vcov(CLC)["Pl:time2:DL","Ll:time3:DL"]+
      2*t2*t4*vcov(CLC)["Pl:time2:DL","Pl:time4:DL"]-2*t2*t4*vcov(CLC)["Pl:time2:DL","Ll:time4:DL"]+2*t2*t5*vcov(CLC)["Pl:time2:DL","Pl:time5:DL"]-2*t2*t5*vcov(CLC)["Pl:time2:DL","Ll:time5:DL"]+2*t2*t6*vcov(CLC)["Pl:time2:DL","Pl:time6:DL"]-2*t2*t6*vcov(CLC)["Pl:time2:DL","Ll:time6:DL"]-
      2*t2*t3*vcov(CLC)["Ll:time2:DL","Pl:time3:DL"]+2*t2*t3*vcov(CLC)["Ll:time2:DL","Ll:time3:DL"]-
      2*t2*t4*vcov(CLC)["Ll:time2:DL","Pl:time4:DL"]+2*t2*t4*vcov(CLC)["Ll:time2:DL","Ll:time4:DL"]-2*t2*t5*vcov(CLC)["Ll:time2:DL","Pl:time5:DL"]+2*t2*t5*vcov(CLC)["Ll:time2:DL","Ll:time5:DL"]-2*t2*t6*vcov(CLC)["Ll:time2:DL","Pl:time6:DL"]+2*t2*t6*vcov(CLC)["Ll:time2:DL","Ll:time6:DL"]-
      2*t3*t3*vcov(CLC)["Pl:time3:DL","Ll:time3:DL"]+
      2*t3*t4*vcov(CLC)["Pl:time3:DL","Pl:time4:DL"]-2*t3*t4*vcov(CLC)["Pl:time3:DL","Ll:time4:DL"]+2*t3*t5*vcov(CLC)["Pl:time3:DL","Pl:time5:DL"]-2*t3*t5*vcov(CLC)["Pl:time3:DL","Ll:time5:DL"]+2*t3*t6*vcov(CLC)["Pl:time3:DL","Pl:time6:DL"]-2*t3*t6*vcov(CLC)["Pl:time3:DL","Ll:time6:DL"]-
      2*t3*t4*vcov(CLC)["Ll:time3:DL","Pl:time4:DL"]+2*t3*t4*vcov(CLC)["Ll:time3:DL","Ll:time4:DL"]-2*t3*t5*vcov(CLC)["Ll:time3:DL","Pl:time5:DL"]+2*t3*t5*vcov(CLC)["Ll:time3:DL","Ll:time5:DL"]-2*t3*t6*vcov(CLC)["Ll:time3:DL","Pl:time6:DL"]+2*t3*t6*vcov(CLC)["Ll:time3:DL","Ll:time6:DL"]-
      2*t4*t4*vcov(CLC)["Pl:time4:DL","Ll:time4:DL"]+2*t4*t5*vcov(CLC)["Pl:time4:DL","Pl:time5:DL"]-2*t4*t5*vcov(CLC)["Pl:time4:DL","Ll:time5:DL"]+2*t4*t6*vcov(CLC)["Pl:time4:DL","Pl:time6:DL"]-2*t4*t6*vcov(CLC)["Pl:time4:DL","Ll:time6:DL"]-
      2*t4*t5*vcov(CLC)["Ll:time4:DL","Pl:time5:DL"]+2*t4*t5*vcov(CLC)["Ll:time4:DL","Ll:time5:DL"]-2*t4*t6*vcov(CLC)["Ll:time4:DL","Pl:time6:DL"]+2*t4*t6*vcov(CLC)["Ll:time4:DL","Ll:time6:DL"]-
      2*t5*t5*vcov(CLC)["Pl:time5:DL","Ll:time5:DL"]+2*t5*t6*vcov(CLC)["Pl:time5:DL","Pl:time6:DL"]-2*t5*t6*vcov(CLC)["Pl:time5:DL","Ll:time6:DL"]-
      2*t5*t6*vcov(CLC)["Ll:time5:DL","Pl:time6:DL"]+2*t5*t6*vcov(CLC)["Ll:time5:DL","Ll:time6:DL"]-
      2*t6*t6*vcov(CLC)["Pl:time6:DL","Ll:time6:DL"]
    CI.PlLl[t,3]=CI.PlLl[t,1]-qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI lower
    CI.PlLl[t,4]=CI.PlLl[t,1]+qnorm(0.975)*sqrt(CI.PlLl[t,2])#95% CI upper
    #right:
    CI.PlLl[t,5]=CLC$coefficients["Pl_ISL"]+CLC$coefficients["Pl:time2"]*t2+CLC$coefficients["Pl:time3"]*t3+CLC$coefficients["Pl:time4"]*t4+CLC$coefficients["Pl:time5"]*t5+CLC$coefficients["Pl:time6"]*t6-
      (CLC$coefficients["Ll_ISL"]+CLC$coefficients["Ll:time2"]*t2+CLC$coefficients["Ll:time3"]*t3+CLC$coefficients["Ll:time4"]*t4+CLC$coefficients["Ll:time5"]*t5+CLC$coefficients["Ll:time6"]*t6)#fitted values
    CI.PlLl[t,6]=1*vcov(CLC)["Pl_ISL","Pl_ISL"]+t2^2*vcov(CLC)["Pl:time2","Pl:time2"]+t3^2*vcov(CLC)["Pl:time3","Pl:time3"]+t4^2*vcov(CLC)["Pl:time4","Pl:time4"]+t5^2*vcov(CLC)["Pl:time5","Pl:time5"]+t6^2*vcov(CLC)["Pl:time6","Pl:time6"]+
      1*vcov(CLC)["Ll_ISL","Ll_ISL"]+t2^2*vcov(CLC)["Ll:time2","Ll:time2"]+t3^2*vcov(CLC)["Ll:time3","Ll:time3"]+t4^2*vcov(CLC)["Ll:time4","Ll:time4"]+t5^2*vcov(CLC)["Ll:time5","Ll:time5"]+t6^2*vcov(CLC)["Ll:time6","Ll:time6"]-
      2*1*1*vcov(CLC)["Pl_ISL","Ll_ISL"]+2*1*t2*vcov(CLC)["Pl_ISL","Pl:time2"]-2*1*t2*vcov(CLC)["Pl_ISL","Ll:time2"]+2*1*t3*vcov(CLC)["Pl_ISL","Pl:time3"]-2*1*t3*vcov(CLC)["Pl_ISL","Ll:time3"]+
      2*1*t4*vcov(CLC)["Pl_ISL","Pl:time4"]-2*1*t4*vcov(CLC)["Pl_ISL","Ll:time4"]+2*1*t5*vcov(CLC)["Pl_ISL","Pl:time5"]-2*1*t5*vcov(CLC)["Pl_ISL","Ll:time5"]+2*1*t6*vcov(CLC)["Pl_ISL","Pl:time6"]-2*1*t6*vcov(CLC)["Pl_ISL","Ll:time6"]-
      2*1*t2*vcov(CLC)["Ll_ISL","Pl:time2"]+2*1*t2*vcov(CLC)["Ll_ISL","Ll:time2"]-2*1*t3*vcov(CLC)["Ll_ISL","Pl:time3"]+2*1*t3*vcov(CLC)["Ll_ISL","Ll:time3"]-
      2*1*t4*vcov(CLC)["Ll_ISL","Pl:time4"]+2*1*t4*vcov(CLC)["Ll_ISL","Ll:time4"]-2*1*t5*vcov(CLC)["Ll_ISL","Pl:time5"]+2*1*t5*vcov(CLC)["Ll_ISL","Ll:time5"]-2*1*t6*vcov(CLC)["Ll_ISL","Pl:time6"]+2*1*t6*vcov(CLC)["Ll_ISL","Ll:time6"]-
      2*t2*t2*vcov(CLC)["Pl:time2","Ll:time2"]+2*t2*t3*vcov(CLC)["Pl:time2","Pl:time3"]-2*t2*t3*vcov(CLC)["Pl:time2","Ll:time3"]+
      2*t2*t4*vcov(CLC)["Pl:time2","Pl:time4"]-2*t2*t4*vcov(CLC)["Pl:time2","Ll:time4"]+2*t2*t5*vcov(CLC)["Pl:time2","Pl:time5"]-2*t2*t5*vcov(CLC)["Pl:time2","Ll:time5"]+2*t2*t6*vcov(CLC)["Pl:time2","Pl:time6"]-2*t2*t6*vcov(CLC)["Pl:time2","Ll:time6"]-
      2*t2*t3*vcov(CLC)["Ll:time2","Pl:time3"]+2*t2*t3*vcov(CLC)["Ll:time2","Ll:time3"]-
      2*t2*t4*vcov(CLC)["Ll:time2","Pl:time4"]+2*t2*t4*vcov(CLC)["Ll:time2","Ll:time4"]-2*t2*t5*vcov(CLC)["Ll:time2","Pl:time5"]+2*t2*t5*vcov(CLC)["Ll:time2","Ll:time5"]-2*t2*t6*vcov(CLC)["Ll:time2","Pl:time6"]+2*t2*t6*vcov(CLC)["Ll:time2","Ll:time6"]-
      2*t3*t3*vcov(CLC)["Pl:time3","Ll:time3"]+
      2*t3*t4*vcov(CLC)["Pl:time3","Pl:time4"]-2*t3*t4*vcov(CLC)["Pl:time3","Ll:time4"]+2*t3*t5*vcov(CLC)["Pl:time3","Pl:time5"]-2*t3*t5*vcov(CLC)["Pl:time3","Ll:time5"]+2*t3*t6*vcov(CLC)["Pl:time3","Pl:time6"]-2*t3*t6*vcov(CLC)["Pl:time3","Ll:time6"]-
      2*t3*t4*vcov(CLC)["Ll:time3","Pl:time4"]+2*t3*t4*vcov(CLC)["Ll:time3","Ll:time4"]-2*t3*t5*vcov(CLC)["Ll:time3","Pl:time5"]+2*t3*t5*vcov(CLC)["Ll:time3","Ll:time5"]-2*t3*t6*vcov(CLC)["Ll:time3","Pl:time6"]+2*t3*t6*vcov(CLC)["Ll:time3","Ll:time6"]-
      2*t4*t4*vcov(CLC)["Pl:time4","Ll:time4"]+2*t4*t5*vcov(CLC)["Pl:time4","Pl:time5"]-2*t4*t5*vcov(CLC)["Pl:time4","Ll:time5"]+2*t4*t6*vcov(CLC)["Pl:time4","Pl:time6"]-2*t4*t6*vcov(CLC)["Pl:time4","Ll:time6"]-
      2*t4*t5*vcov(CLC)["Ll:time4","Pl:time5"]+2*t4*t5*vcov(CLC)["Ll:time4","Ll:time5"]-2*t4*t6*vcov(CLC)["Ll:time4","Pl:time6"]+2*t4*t6*vcov(CLC)["Ll:time4","Ll:time6"]-
      2*t5*t5*vcov(CLC)["Pl:time5","Ll:time5"]+2*t5*t6*vcov(CLC)["Pl:time5","Pl:time6"]-2*t5*t6*vcov(CLC)["Pl:time5","Ll:time6"]-
      2*t5*t6*vcov(CLC)["Ll:time5","Pl:time6"]+2*t5*t6*vcov(CLC)["Ll:time5","Ll:time6"]-
      2*t6*t6*vcov(CLC)["Pl:time6","Ll:time6"]
    CI.PlLl[t,7]=CI.PlLl[t,5]-qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI lower
    CI.PlLl[t,8]=CI.PlLl[t,5]+qnorm(0.975)*sqrt(CI.PlLl[t,6])#95% CI upper
  }
  CI.PlLl=as.data.frame(CI.PlLl)
  CI.PlLl$t=c(1961:2019)
  if (pe==1) {CI.PlLl=CI.PlLl[1:(10+t0),]}
  if (pe==2) {CI.PlLl=CI.PlLl[(11+t0):(20+t0),]}
  if (pe==3) {CI.PlLl=CI.PlLl[(21+t0):(30+t0),]}
  if (pe==4) {CI.PlLl=CI.PlLl[(31+t0):(40+t0),]}
  if (pe==5) {CI.PlLl=CI.PlLl[(41+t0):(50+t0),]}
  if (pe==6) {CI.PlLl=CI.PlLl[(51+t0):59,]}
  CI.PlLl
}#wo Pl:time1:DL,Ll:time1:DL

#Z-test functions:
ZtestfunT12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTD12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2,":DL")]-CLC$coefficients[paste0("Ll:time",2,":DL")]#mean
  v=vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Pl:time",2,":DL")]+vcov(CLC)[paste0("Ll:time",2,":DL"),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Ll:time",2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunD12=function(CLC){
  m=CLC$coefficients[paste0("Pl:time",2)]-CLC$coefficients[paste0("Ll:time",2)]+
    CLC$coefficients[paste0("Pl:time",2,":DL")]-CLC$coefficients[paste0("Ll:time",2,":DL")]#mean
  v=vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2)]+vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2)]+
    vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Pl:time",2,":DL")]+vcov(CLC)[paste0("Ll:time",2,":DL"),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2)]+2*vcov(CLC)[paste0("Pl:time",2),paste0("Pl:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",2),paste0("Pl:time",2,":DL")]+2*vcov(CLC)[paste0("Ll:time",2),paste0("Ll:time",2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",2,":DL"),paste0("Ll:time",2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunT=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunTD=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1,":DL")]-CLC$coefficients[paste0("Ll:time",per1,":DL")]-
        CLC$coefficients[paste0("Pl:time",per2,":DL")]+CLC$coefficients[paste0("Ll:time",per2,":DL")])#mean
  v=vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per1,":DL")]+vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per1,":DL")]+
    vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Pl:time",per2,":DL")]+vcov(CLC)[paste0("Ll:time",per2,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2,":DL")]-2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Ll:time",per2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}
ZtestfunD=function(CLC,per1,per2){
  m=-(CLC$coefficients[paste0("Pl:time",per1)]-CLC$coefficients[paste0("Ll:time",per1)]+
        CLC$coefficients[paste0("Pl:time",per1,":DL")]-CLC$coefficients[paste0("Ll:time",per1,":DL")]-
        CLC$coefficients[paste0("Pl:time",per2)]+CLC$coefficients[paste0("Ll:time",per2)]-
        CLC$coefficients[paste0("Pl:time",per2,":DL")]+CLC$coefficients[paste0("Ll:time",per2,":DL")])#mean
  v=vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1)]+vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1)]+
    vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per1,":DL")]+vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per1,":DL")]+
    vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2)]+vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2)]+
    vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Pl:time",per2,":DL")]+vcov(CLC)[paste0("Ll:time",per2,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1)]+2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per1,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2)]-2*vcov(CLC)[paste0("Pl:time",per1),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per1,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per1,":DL")]+2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2)]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Ll:time",per1),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per1,":DL")]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2)]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2)]-2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Pl:time",per1,":DL"),paste0("Ll:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2)]-
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per1,":DL"),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2)]+2*vcov(CLC)[paste0("Pl:time",per2),paste0("Pl:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Ll:time",per2),paste0("Pl:time",per2,":DL")]+
    2*vcov(CLC)[paste0("Ll:time",per2),paste0("Ll:time",per2,":DL")]-
    2*vcov(CLC)[paste0("Pl:time",per2,":DL"),paste0("Ll:time",per2,":DL")]#variance
  se=sqrt(v)#standard error
  z=m/sqrt(v)#Z score
  p=2*pnorm(-abs(z))#p-value != (two tails)
  c(m,se,p)
}

#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
de$time1=ifelse(de$time<=10+t0,1,0)
de$time2=ifelse(de$time>10+t0&de$time<=20+t0,1,0)
de$time3=ifelse(de$time>20+t0&de$time<=30+t0,1,0)
de$time4=ifelse(de$time>30+t0&de$time<=40+t0,1,0)
de$time5=ifelse(de$time>40+t0&de$time<=50+t0,1,0)
de$time6=ifelse(de$time>50+t0,1,0)

#regression:
sort(unique(de$Ey[de$DL==1]))#full coverage DL, hence full interactions
sort(unique(de$Ey[de$DL==0]))#full coverage non DL, hence full interactions

CLC_lses103_DEU=clogit(Va ~ Pl:time2+Ll:time2+Pl:time3+Ll:time3+Pl:time4+Ll:time4+Pl:time5+Ll:time5+Pl:time6+Ll:time6+
                         Pl:time2:DL+Ll:time2:DL+Pl:time3:DL+Ll:time3:DL+Pl:time4:DL+Ll:time4:DL+Pl:time5:DL+Ll:time5:DL+Pl:time6:DL+Ll:time6:DL+
                         +Pl_AUS+Ll_AUS+Pl_AUT+Ll_AUT+Pl_CAN+Ll_CAN+Pl_DEU+Ll_DEU+Pl_DNK+Ll_DNK+Pl_ESP+Ll_ESP+Pl_FIN+Ll_FIN+Pl_GBR+Ll_GBR+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_ISR+Ll_ISR+Pl_ITA+Ll_ITA+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_NZL+Ll_NZL+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
                       +strata(Esalt), robust=T, data=de, method="efron")#regression
names(CLC_lses103_DEU$coefficients)[names(CLC_lses103_DEU$coefficients)=="time2:Ll"]="Ll:time2"
names(CLC_lses103_DEU$coefficients)[names(CLC_lses103_DEU$coefficients)=="time2:Ll:DL"]="Ll:time2:DL"


##Table F3##
#hypothesis testing:
stargazer(CLC_lses103_DEU,no.space = T,single.row = T)
#drop low (DL==0):
ZtestfunT12(CLC_lses103_DEU)
ZtestfunT(CLC_lses103_DEU,2,3)
ZtestfunT(CLC_lses103_DEU,3,4)
ZtestfunT(CLC_lses103_DEU,4,5)
ZtestfunT(CLC_lses103_DEU,5,6)
#drop high (DL==1):
ZtestfunD12(CLC_lses103_DEU)
ZtestfunD(CLC_lses103_DEU,2,3)
ZtestfunD(CLC_lses103_DEU,3,4)
ZtestfunD(CLC_lses103_DEU,4,5)
ZtestfunD(CLC_lses103_DEU,5,6)
#difference in drop between high and low (DL==1 vs DL==0):
ZtestfunTD12(CLC_lses103_DEU)
ZtestfunTD(CLC_lses103_DEU,2,3)
ZtestfunTD(CLC_lses103_DEU,3,4)
ZtestfunTD(CLC_lses103_DEU,4,5)
ZtestfunTD(CLC_lses103_DEU,5,6)

#plots (median cty):
# tikz(paste0("plot_","CLC_lses103_DEU.tex"),width=4, height=3)
plot( ggplot() +ggtitle("") +xlab("Year") +ylab("Party/Leader Effect") + coord_cartesian(ylim=c(0.2001,0.64)) + scale_y_continuous(expand=c(0,0)) + xlim(1960,2020) +
        theme(panel.background=element_blank(), panel.grid.major=element_line(size=0.15,linetype='solid',colour = "grey"),
              panel.border=element_rect(fill="transparent",size = 0.3), text=element_text(size=12), plot.title=element_text(hjust = 0.5)) +
        geom_histogram(aes(x=Ey,y=((Eyn*(0.64-0.2001)+0.2001))),stat='identity',binwidth = 1,color="gray85",fill="gray85",data=dyALL) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses103_DEU,0,1)) +#(high)
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses103_DEU,0,2)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses103_DEU,0,3)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses103_DEU,0,4)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses103_DEU,0,5)) +
        geom_smooth(aes(x=t,y=V1,ymin=V3,ymax=V4), stat="identity", color="red", lty="44", lwd=3, CI.PlLllses10CGfun(CLC_lses103_DEU,0,6)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses103_DEU,0,1)) +#(low)
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses103_DEU,0,2)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses103_DEU,0,3)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses103_DEU,0,4)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses103_DEU,0,5)) +
        geom_smooth(aes(x=t,y=V5,ymin=V7,ymax=V8), stat="identity", color="blue", lwd=2, CI.PlLllses10CGfun(CLC_lses103_DEU,0,6)) )
# dev.off()


##Table I14##
##table full results:
stargazer(CLC_lses103_ea_0,CLC_lses103_LR25_0,CLC_lses103_DEU)


#RC DOUBLE ELECTIONS:----

##read data:
load("d29_doubleEs.RData")


##base for country-slope FE:
#Ec:Pl FE:
for (i1 in 1:length(sort(unique(dd$Ec)))) {#creating dummy variables Ec:Pl (for FE)
  dd[[paste0("Pl_",sort(unique(dd$Ec))[i1])]]=ifelse(dd$Ec==sort(unique(dd$Ec))[i1],dd$Pl,0)#dd$Pl
}
#Ec:Ll FE:
for (i1 in 1:length(sort(unique(dd$Ec)))) {#creating dummy variables Ec:Ll (for FE)
  dd[[paste0("Ll_",sort(unique(dd$Ec))[i1])]]=ifelse(dd$Ec==sort(unique(dd$Ec))[i1],dd$Ll,0)#dd$Ll
}
#create text for regression function:
txt=""
for (i1 in 1:length(sort(unique(dd$Ec)))) {#creating function text for dummy variables Es:alt except for alt=1
  txt=paste(txt,paste0("Pl_",sort(unique(dd$Ec))[i1]),sep="+")
  txt=paste(txt,paste0("Ll_",sort(unique(dd$Ec))[i1]),sep="+")
}
txt

##analyze same elections:
#Es with same national election study data (same nobs):
same_Es <- dd %>%
  group_by(Es, Ed) %>%
  summarise(n = n(), .groups = "drop") %>%
  pivot_wider(names_from = Ed, values_from = n, names_prefix = "Survey_") %>%
  filter(Survey_0 == Survey_1) %>%
  pull(Es)
#total Es:
sort(unique(dd$Es))
length(sort(unique(dd$Es)))
#Es with same data:
same_Es
length(same_Es)


##regressions and table:
CLC1=clogit(Va ~ Pl*Ed+Ll*Ed
            +Pl_DEU+Ll_DEU+Pl_FIN+Ll_FIN+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
            +strata(Esalt), robust=T, data=dd, method="efron")#regression
CLC2=clogit(Va ~ Pl*Ed+Ll*Ed
            +Pl_DEU+Ll_DEU+Pl_FIN+Ll_FIN+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
            +strata(Esalt), robust=T, data=dd[!(dd$Es %in% c(same_Es)),], method="efron")#regression


##regressions interaction with time:
#time variables:
t0=0#starting value for thresholds (-4,-2,0,2,4)
# de$time1=ifelse(de$time<=10+t0,1,0)
dd$time2=ifelse(dd$time>10+t0&dd$time<=20+t0,1,0)
dd$time3=ifelse(dd$time>20+t0&dd$time<=30+t0,1,0)
dd$time4=ifelse(dd$time>30+t0&dd$time<=40+t0,1,0)
dd$time5=ifelse(dd$time>40+t0&dd$time<=50+t0,1,0)
dd$time6=ifelse(dd$time>50+t0,1,0)
#regressions:
CLC3=clogit(Va ~ Pl*Ed*time+Ll*Ed*time
            +Pl_DEU+Ll_DEU+Pl_FIN+Ll_FIN+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
            +strata(Esalt), robust=T, data=dd, method="efron")#regression
CLC4=clogit(Va ~ Pl*Ed*time+Ll*Ed*time
            +Pl_DEU+Ll_DEU+Pl_FIN+Ll_FIN+Pl_GRC+Ll_GRC+Pl_IRL+Ll_IRL+Pl_ISL+Ll_ISL+Pl_NLD+Ll_NLD+Pl_NOR+Ll_NOR+Pl_PRT+Ll_PRT+Pl_SWE+Ll_SWE
            +strata(Esalt), robust=T, data=dd[!(dd$Es %in% c(same_Es)),], method="efron")#regression


##Table I1##
##table:
stargazer(CLC1,CLC3,CLC2,CLC4)

